1, define a GPS management class:
GPSLocationManager.h:
#import<Foundation/Foundation.h>#import "SingletonTemplate.h"//GPS Positioning#import<CoreLocation/CoreLocation.h>#import<MapKit/MapKit.h>@interfaceGpslocationmanager:nsobject <CLLocationManagerDelegate>Singleton_for_header (Gpslocationmanager)#pragmaMark-gps locating a successful and failed callback block/** * GPS location succeeded and failed block*/@property (nonatomic, copy)void(^Gpssuccessblock) (cllocationdistance longitude, cllocationdistance latitude); @property (nonatomic, copy)void(^Gpsfailureblock) ();- (void) Addgpssuccesscallback: (void(^) (cllocationdistance longitude, cllocationdistance latitude)) callback;- (void) Addgpsfailurecallback: (void(^) ()) callback;#pragmaMark-enable/stop GPS positioning/** * Turn on GPS positioning * Stop GPS location*/- (void) startgpslocation;- (void) stopgpslocation;#pragmaMark-Get the current location by latitude coordinates/** * Get the current location by latitude coordinates * * @param longitude longitude * @param latitude latitude*/- (void) Gpslocationinfobylong: (cllocationdistance) Longitude Lat: (cllocationdistance) latitude Locationinfo: (void(^) (Clplacemark *gpsinfo)) info;@end
GPSLOCATIONMANAGER.M File Contents:
#import "GPSLocationManager.h"#import "MHLocationConverter.h"@interfaceGpslocationmanager () @property (Strong, nonatomic) Cllocationmanager*locationmanager;//Location Manager@property (Assign, nonatomic) cllocationdistance longitude;//Longitude@property (Assign, nonatomic) cllocationdistance latitude;//Latitude@property (Strong, nonatomic) Clgeocoder *geocoder;//Location Anti-compilation@end@implementationGpslocationmanagersingleton_for_class (Gpslocationmanager)#pragmaMark-turn on GPS positioning-(void) startgpslocation{//Check if location services is turned on if(![Cllocationmanager locationservicesenabled]) { //Location Not availableDef_debug (@"location service is not turned on"); return; } Self.locationmanager=[[Cllocationmanager alloc] init]; //whether IOS 8 is judged if([Self.locationmanager respondstoselector: @selector (requestalwaysauthorization)]) {//[Self.locationmanager requestalwaysauthorization]; //Permanent License[Self.locationmanager requestwheninuseauthorization];//Licensing in use} self.locationmanager.Delegate=Self ; //Required Positional accuracySelf.locationManager.desiredAccuracy =kcllocationaccuracybest; //Specify the minimum distance to update with metersSelf.locationManager.distanceFilter =Kcldistancefilternone; //Start Positioning[Self.locationmanager startupdatinglocation];}- (void) Locationmanager: (Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status{Def_debug (@"Device Rotation point:%u", status);}- (void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) error{Def_debug (@"location failed:%@", error); //GPS Location Failure if(Self.gpsfailureblock) {self.gpsfailureblock (); }}- (void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation *) newlocation fromlocation: ( Cllocation *) oldlocation{//WGS-84 coordinates converted to BD-09 coordinatesCllocationcoordinate2d coord =[Mhlocationconverter wgs84ToBd09:newLocation.coordinate]; Def_debug (@"GPS location converted to Baidu map longitude:%f, Latitude:%f", Coord.longitude, coord.latitude); //get converted latitude and longitudeSelf.longitude =Coord.longitude; Self.latitude=Coord.latitude; //GPS Positioning Success if(Self.gpssuccessblock) {Self.gpssuccessblock (self.longitude, self.latitude); }}#pragmaMark-Stop positioning-(void) stopgpslocation{[Self.locationmanager stopupdatinglocation];}#pragmaMark-Gets the current location through latitude coordinates-(void) Gpslocationinfobylong: (cllocationdistance) Longitude Lat: (cllocationdistance) latitude Locationinfo: (void(^) (Clplacemark *gpsinfo)) info{/** * Anti-compilation latitude and longitude*/ //The incoming coordinate system is BD-09.cllocationcoordinate2d coord; Coord.longitude=longitude; Coord.latitude=latitude; //turn back to GCJ-02 from BD-09 coordinates, then position conversionCllocationcoordinate2d Newcoord =[Mhlocationconverter Bd09togcj02:coord]; //Prevent circular references__unsafe_unretainedtypeof(self) _self =Self ; _self.geocoder=[[Clgeocoder alloc] init]; [_self.geocoder reversegeocodelocation:[[cllocation alloc] initWithLatitude:newCoord.latitude Longitude: Newcoord.longitude] Completionhandler:^ (Nsarray *placemarks, Nserror *error) { //data information for the current location if(Error = = Nil && placemarks.count >0) {Clplacemark*placemark = [Placemarks objectatindex:0]; Def_debug (@"positioning Result:%@", Placemark); //Block Callback if(info) {info (placemark); } } Else if(Error = = Nil && Placemarks.count = =0) {Def_debug (@"Longitude and latitude inverse compilation no results returned"); } Else if(Error! =Nil) {Def_debug (@"Longitude and latitude inverse compilation error:%@", error); } }];}#pragmamark-block-(void) Addgpssuccesscallback: (void(^) (cllocationdistance longitude, cllocationdistance latitude)) callback{Self.gpssuccessblock=callback;}- (void) Addgpsfailurecallback: (void(^) ()) callback{Self.gpsfailureblock=callback;}@end
2. Implement Gpslocate method in Appdelegate, open position
#pragmaMark-gps Positioning-(void) gpslocate{//Prevent circular references__unsafe_unretainedtypeof([Gpslocationmanager Sharedgpslocationmanager]) GPS =[Gpslocationmanager Sharedgpslocationmanager]; //Turn on positioning[GPS startgpslocation]; //Locating ResultsGps.gpsfailureblock = ^(){ //failed to locate }; Gps.gpssuccessblock= ^(cllocationdistance longitude, cllocationdistance latitude) {//Stop Positioning[GPS stopgpslocation]; //Positioning SuccessNSLog (@"location succeeded, Longitude:%f, Latitude:%f", longitude, latitude); //Inverse Compilation Area[GPS Gpslocationinfobylong:longitude Lat:latitude locationinfo:^ (Clplacemark *gpsinfo) { //NSLog (@"Writing One, current location information:%@", Gpsinfo); //get the cityNSString *city =gpsinfo.locality; if(!City ) { //the city information of the four municipalities can not be obtained by locality, but only by obtaining the province's method (if it is empty, it is known as the municipality)City =Gpsinfo.administrativearea; } NSLog (@"City =%@", city); Nsmutablestring*mutable =[[Nsmutablestring alloc] initwithstring:city]; if([mutable rangeofstring:@"Districts"].location! =nsnotfound) {[Mutable deletecharactersinrange:nsmakerange ([mutable length]-3,3)]; City=mutable; } }]; };}
The realization of GPS positioning