In IOS8, if you use the system's own map, first add two fields to the plist file: Nslocationalwaysusagedescription and Nslocationwheninuseusagedescription Type is string type second note: supported interface orientations If there is no existence then create (array type);
Use the map to first introduce the necessary agent after the Mapkit library <CLLocationManagerDelegate>
@interface Viewcontroller () <CLLocationManagerDelegate> @property (nonatomic, strong) Mkmapview *mapview;@ Property (Nonatomic, Strong) Cllocationmanager *locationmanager; @property (nonatomic, strong) Cllocation *namelocation ; @end
To create a map:
Self.mapview = [[Mkmapview alloc] Initwithframe:cgrectmake (0,, Self.view.frame.size.width, self.view.frame.size.height-140)]; Self.mapView.showsUserLocation = YES; Navigate to your location //self.mapview.maptype = mkmaptypesatellite;//Map Type Here is the satellite specific map type See API [Self.view addsubview: Self.mapview]; Self.locationmanager = [[Cllocationmanager alloc] init]; Self.locationManager.delegate = self; _locationmanager.desiredaccuracy = Kcllocationaccuracybest; Control the positioning accuracy, the higher the power consumption of the greater. _locationmanager.distancefilter = 10;//Control location Service Update frequency. Unit is "M"
[Self.locationmanager startupdatinglocation];
In IOS8, you may not be able to pop up a alertview that allows you to use geolocation.
if ([_locationmanager respondstoselector: @selector (requestwheninuseauthorization)]) {[_locationmanager RequestWhen Inuseauthorization]; }//determine if the service is on-(void) Locationmanager: (Cllocationmanager *) Manager Didchangeauthorizationstatus: ( Clauthorizationstatus) Status {if ([_locationmanager respondstoselector: @selector (Requestwheninuseauthoriz ation)] && status! = kclauthorizationstatusnotdetermined && Status! = Kclauthorizationstatusauthorizedwheninuse) | | (! [_locationmanager respondstoselector: @selector (requestwheninuseauthorization)] && Status! = kclauthorizationstatusnotdetermined && status = kclauthorizationstatusauthorized)) {Nsstr ing *message = @ "Your phone is not currently open for location services, if you want to open Location services, please set the location service function"; Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Cannot locate" Message:message delegate:nil cancelbuttontitle:@ " Determine "Otherbuttontitles:nil"; [Alertview show]; }else {[_locatioNmanager Startupdatinglocation]; }}
Create a PIN class: customannotation inherits from NSObject
customannotation.h// map//// Created by Gao Qi on 15/4/6.// Copyright (c) 2015 Gaoqi. All rights reserved.//#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface customannotation:nsobject<mkannotation>{ cllocationcoordinate2d coordinate; NSString *title; NSString *subtitle;} -(ID) initwithcoordinate: (cllocationcoordinate2d) coords; @property (nonatomic, ReadOnly) cllocationcoordinate2d coordinate; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *subtitle; @end
customannotation.m// map//// Created by Gao Qi on 15/4/6.// Copyright (c) 2015 Gaoqi. All rights reserved.//#import "CustomAnnotation.h" @implementation customannotation@synthesize coordinate, title, subtitle;-(ID) initwithcoordinate: (cllocationcoordinate2d) coords{ if (self = [super init]) { coordinate = coords; } return self;} @end
Add pin-(void) Createannotationwithcoords: (cllocationcoordinate2d) coords {customannotation *annotation = [[ Customannotation alloc] initwithcoordinate:coords]; Click on the pin to get the specific street address via self.namelocation clgeocoder *clgeocoder = [[Clgeocoder alloc] init]; [Clgeocoder reverseGeocodeLocation:self.nameLocation Completionhandler: ^ (nsarray *placemarks,nserror *error) { For (Clplacemark *placemark in placemarks) {nsdictionary *addressdic=placemark.addressdictionary; NSString *state=[addressdic objectforkey:@ "state"]; NSString *city=[addressdic objectforkey:@ "City"]; NSString *sublocality=[addressdic objectforkey:@ "sublocality"]; NSString *street=[addressdic objectforkey:@ "Street"]; Annotation.title = [NSString stringwithformat:@ "%@%@%@%@", state, City, Sublocality, Street]; Click the pin to get the specific street address}}]; [Self.mapviewAddannotation:annotation];}
The next step is to locate
By the city name with a pin positioning, which is through a button and a TextField (Self.text) to search the following is the button click Method is actually positioned in place of the pin (can only be located in the country)-(void) buttonclick{//PIN located in the Forbidden City//cllocationcoordinate2d coords = Cllocationcoordinate2dmake (39.915352,116.397105);// float Zoomlevel = 0.02;//mkcoordinateregion region = Mkcoordinateregionmake (coords, Mkcoordinatespanmake (ZoomLevel, Zo Omlevel));//[Self.mapview Setregion:[self.mapview regionthatfits:region] animated:yes];//////[Self CreateA Nnotationwithcoords:coords]; Get geolocation by name and pin position Clgeocoder *mygeocoder = [[Clgeocoder alloc] init]; [Mygeocoder geocodeAddressString:self.text.text completionhandler:^ (Nsarray *placemarks, Nserror *error) {if ([Placemarks count] > 0 && error = = nil) {//NSLog (@ "Found%lu placemark (s).", (U nsigned long) [Placemarks Count]); Clplacemark *firstplacemark = [Placemarks objectatindex:0]; NSLog (@ "longitude =%f", FirstPlacemark.location.coordinate.lonGitude); NSLog (@ "Latitude =%f", firstPlacemark.location.coordinate.latitude); Geographic information (province, city, district, Street) is sent back to the pin by latitude where then the pin is positioned self.namelocation = [[Cllocation alloc] Initwithlatitude:firstplacema Rk.location.coordinate.latitude Longitude:firstPlacemark.location.coordinate.longitude]; P.S. This place, I don't know if it's right. (Click the Search button will buffer for a period of time the problem is not the location problem network slow?) Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{cllocationcoordinate2d COO RDS = Cllocationcoordinate2dmake (FirstPlacemark.location.coordinate.latitude, FirstPlacemark.location.coordinate.longitude); float zoomlevel = 0.02; Mkcoordinateregion region = Mkcoordinateregionmake (coords, Mkcoordinatespanmake (Zoomlevel, zoomlevel)); [Self.mapview Setregion:[self.mapview regionthatfits:region] animated:yes]; Move to location Dispatch_async (dispatch_get_mAin_queue (), ^{[self createannotationwithcoords:coords]; }); }); } else if ([placemarks count] = = 0 && Error = = nil) {NSLog (@ "Found no placemarks."); } else if (Error! = nil) {NSLog (@ "An error occurred =%@", error); } }];}
There are proxy methods and things like that.
-(void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: ( Cllocation *) newlocation fromlocation: (cllocation *) oldlocation {//proxy method print latitude and longitude [Self.locationmanager stopupdatingloc Ation]; NSString *strlat = [NSString stringwithformat:@ "%.4f", NewLocation.coordinate.latitude]; NSString *strlng = [NSString stringwithformat:@ "%.4f", NewLocation.coordinate.longitude]; NSLog (@ "Lat:%@ Lng:%@", Strlat, STRLNG); The map moves to the current position cllocationcoordinate2d coords = Cllocationcoordinate2dmake (newlocation.coordinate.latitude,newlocation . coordinate.longitude); float zoomlevel = 0.02; Mkcoordinateregion region = Mkcoordinateregionmake (Coords,mkcoordinatespanmake (Zoomlevel, zoomLevel)); [Self.mapview Setregion:[self.mapview regionthatfits:region] animated:yes]; }//Error Report-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error {NSLog (@ "locerror:%@") , error); }
Use of the IOS8 map