Ios--corelocation Geo-geocoding
Operation Steps
Corelocation/corelocation.h
@property (nonatomic, strong) Clgeocoder *geocoder;
3.1 Lazy Load Create Geocoder property
-(Clgeocoder *) geocoder{ if (!_geocoder) { self.geocoder = [[Clgeocoder alloc] init]; } return _geocoder;}
3.2 geocoding
Geo-(void) dili{//geocoding method NSString *straddress = @ "Beijing"; [Self.geocoder geocodeaddressstring:straddress completionhandler:^ (Nsarray *placemarks, NSError *error) {if (error) {//has error NSLog (@ "Cannot find this address"); } else {///code succeeds//takes out the front address clplacemark *pm = [Placemarks firstobject]; Remove the latitude and longitude of cgfloat latitude = pm.location.coordinate.latitude; CGFloat longitude = pm.location.coordinate.longitude; NSLog (@ "A total of%d addresses found", placemarks.count); For (Clplacemark *pm in Placemarks) {NSLog (@ "-----address starts----"); NSLog (@ "%f%f%@", Pm.location.coordinate.latitude, Pm.location.coordinate.longitude, pm.name); [Pm.addressdictionary enumeratekeysandobjectsusingblock:^ (ID key, id obj, BOOL *stop) {NSLog (@ "%@%@", key, obj); }]; NSLog (@ "-----address end----"); } } }];}
3.3 Anti-geocoding
Anti-geo-(void) fandili{//anti-geocoding//1. Position cllocationdegrees latitude = 114.0; Cllocationdegrees longitude = 45.0; Cllocation *loc = [[Cllocation alloc] Initwithlatitude:latitude longitude:longitude]; 2. Anti-geocoding [Self.geocoder reversegeocodelocation:loc completionhandler:^ (Nsarray *placemarks, NSError *error) { if (error) {//There is an error (address disorderly input) NSLog (@ "Cannot find this address"); } else {///code succeeds//takes out the front address clplacemark *pm = [Placemarks firstobject]; NSLog (@ "%@", pm.name); Set the specific address NSLog (@ "A total of%ld addresses found", (long) placemarks.count); For (Clplacemark *pm in Placemarks) {NSLog (@ "-----address starts----"); NSLog (@ "%f%f%@", Pm.location.coordinate.latitude, Pm.location.coordinate.longitude, pm.name); [Pm.addressdictionary enumeratekeysandobjectsusingblock:^ (ID key, id obj, BOOL *stop) {NSLog (@ "%@%@ ", key, obj); }]; NSLog (@ "-----address end----"); } } }];}
3.4 Calling methods
-(void) viewdidload { [super viewdidload]; [Self Dili]; [Self Fandili];}
iOS geo-geocoding--corelocation