Sometimes it is possible to use some of the features of the map, such as to get the current city, and then return the corresponding data according to the city, without the need for other operations, each time to write a few lines of code, so you do a small package, later may add new features to adapt to the various needs of the app (make it stronger)
GitHub Package Address: https://github.com/iOSSinger/SGLocation
Core code:
How to use:
//One line of code gets the current city[Loc setgetcity:^ (NSString *City ) {NSLog (@"%@", city); }]; //One line of code gets the current position[Loc setgetlocation:^ (NSString *Location ) {NSLog (@"%@", location); }]; //A line of code gets the current coordinates[Loc setgetcoordinate:^(cllocationcoordinate2d coodinate) {NSLog (@"%f-%f", Coodinate.latitude,coodinate.longitude); }];
Implementation of the Code, in fact, the value is passed back with block, without the need to use the proxy
#import "SGLocation.h"@interfaceSglocation () <CLLocationManagerDelegate>@property (nonatomic,strong) Cllocationmanager*Manager;@end@implementationsglocationStaticSglocation *_instance;+ (Instancetype) Allocwithzone: (struct_nszone *) zone{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instance=[Super Allocwithzone:zone]; }); return_instance;}+(instancetype) sharelocation{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instance=[[Self alloc] init]; }); return_instance;}-(Cllocationmanager *) manager{if(_manager = =Nil) {_manager=[[Cllocationmanager alloc] init]; _manager.Delegate=Self ; [_manager requestwheninuseauthorization]; } return_manager;}- (void) Setgetcity: (void(^) (NSString *)) getcity{_getcity=getcity; [Self.manager startupdatinglocation];}- (void) Setgetcoordinate: (void(^) (cllocationcoordinate2d)) getcoordinate{_getcoordinate=getcoordinate; [Self.manager startupdatinglocation];}- (void) Setgetlocation: (void(^) (NSString *)) getlocation{_getlocation=getLocation; [Self.manager startupdatinglocation];}- (void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) locations{cllocation*loc =[Locations Lastobject]; //Anti-geo-codingClgeocoder *geocoder =[[Clgeocoder alloc] init]; [Geocoder reversegeocodelocation:loc Completionhandler:^ (Nsarray *placemarks, Nserror *error) { if(placemarks.count) {//get current CityClplacemark *mark =Placemarks.firstobject; Nsdictionary*dict =[Mark Addressdictionary]; NSString*city = [Dict objectforkey:@" State"]; //City if(self.getcity) {self.getcity (city); [Self.manager stopupdatinglocation]; } //coordinates if(self.getcoordinate) {self.getcoordinate (mark.location.coordinate); [Self.manager stopupdatinglocation]; } //Location Information if(self.getlocation) {self.getlocation (mark.name); [Self.manager stopupdatinglocation]; } } }];}@end
iOS get current city, location, coordinates, latitude