First, the basic concept
Geocoding: Obtain specific location information (such as latitude and longitude, full name of address, etc.) based on a given place name
Anti-geocoding: Obtain specific location information based on a given latitude and longitude
Each instance of the encoder clgeocoder, while processing only one task, executes asynchronously.
Ii. Basic Methods
1. Geocoding Methods
-(void) geocodeaddressstring: (NSString *) addressstring Completionhandler: (Clgeocodecompletionhandler) Completionhandler;
2. Anti-geo-coding methods
-(void) Reversegeocodelocation: (cllocation *) location Completionhandler: (Clgeocodecompletionhandler) Completionhandler;
3. Clgeocodecompletionhandler is called when geo-geocoding is complete
typedef void (^clgeocodecompletionhandler) (Nsarray *placemarks, Nserror *error);
This block passes 2 parameter error: When the coding error (such as the code does not have specific information) has the value Placemarks: Inside contains the Clplacemark object
Clplacemark literally means a landmark, encapsulating detailed address location information
3.1 Clplacemark Properties
@property (nonatomic, readonly) cllocation *location; location
@property (nonatomic, readonly) clregion *region;
@property (nonatomic, readonly) nsdictionary *addressdictionary; Detailed address information
@property (nonatomic, readonly) nsstring *name; address name
@property (nonatomic, readonly) NSString *locality; city
Geo-coding and anti-geocoding implementation
1. Import the <CoreLocation/CoreLocation.h> framework first, then import the #import <CoreLocation/CoreLocation.h> header file
2. Create an Encoder object and lazy-load
@property (Nonatomic,strong) clgeocoder *Geocoder; -(Clgeocoder *) geocoder{ if (! _geocoder) { = [[Clgeocoder alloc] init]; } return _geocoder;}
3. Encoding method implementation
- (void) geocod{NSLog (@"ddddd"); //1. Get the address you enteredNSString *address =_addressfield.text; if(Address.length = =0)return; //2. Start Coding[Self.geocoder geocodeaddressstring:address completionhandler:^ (Nsarray *placemarks, NSError *error) { if(Error | | placemarks.count = =0) {_detailaddresslabel.text=@"the address you entered could not be found"; } Else{//Successful encoding (location information is found)//output All the landmark information you are looking for for(Clplacemark *placeminchplacemarks) {NSLog (@"name:%@ locality:%@ country:%@ postalcode:%@", Placem.name, Placem.locality,placem.country, Placem.postalcode); } //Show the front landmark informationClplacemark *firstplacemark =[Placemarks Firstobject]; _detailaddresslabel.text=Firstplacemark.name; Cllocationdegrees Latitude=FirstPlacemark.location.coordinate.latitude; Cllocationdegrees Longitude=FirstPlacemark.location.coordinate.longitude; _latitudelabel.text= [NSString stringWithFormat:@"%.2f", Latitude]; _longgitudelable.text= [NSString stringWithFormat:@"%.2f", longitude]; } }];}
4. Anti-geocoding implementation
NSString *longtitudetext =_longgitudefield.text; NSString*latitudetext =_latitudefield.text; if(Longtitudetext.length = =0|| Latitudetext.length = =0)return; Cllocationdegrees Latitude=[Latitudetext Doublevalue]; Cllocationdegrees longtitude=[Longtitudetext Doublevalue]; //Start Reverse CodingCllocation *location =[[Cllocation alloc] Initwithlatitude:latitude longitude:longtitude]; [Self.geocoder reversegeocodelocation:location Completionhandler:^ (Nsarray *placemarks, Nserror *error) { if(Error | | placemarks.count = =0) {_reversedetailaddresslabel.text=@"the address you entered could not be found"; } Else{ for(Clplacemark *placeminchplacemarks) {NSLog (@"name:%@ locality:%@ country:%@ postalcode:%@", Placem.name, Placem.locality,placem.country, Placem.postalcode); } Clplacemark*firstplacemark =[Placemarks Firstobject]; _reversedetailaddresslabel.text=Firstplacemark.name; Cllocationdegrees Latitude=FirstPlacemark.location.coordinate.latitude; Cllocationdegrees Longitude=FirstPlacemark.location.coordinate.longitude; Self.latitudeField.text= [NSString stringWithFormat:@"%.2f", Latitude]; Self.longgitudeField.text= [NSString stringWithFormat:@"%.2f", longitude]; } }];
:
Geocoding and anti-geocoding