IOS development expansion-CoreLocation geocode
1. CLGeocoder: Geographic encoder. Geo is short for Geography. 1. CLGeocoder can be used to complete "geocoding" and "anti-geocoding" geocoding: Obtain specific location information (such as longitude and latitude, and the full name of the address) based on the given place name. Anti-geocoding: obtain the specific location information based on the given longitude and latitude (1) geocoding method-(void) geocodeAddressString :( NSString *) addressString completionHandler :( CLGeocodeCompletionHandler) completionHandler; (2) anti-Geographic Encoding Method-(void) reverseGeocodeLocation :( CLLocation *) location completionHandler :( CLGeocodeCompletionHandler) completionHandler; 2. CLGeocodeCompletionHandler when the geographic/anti-Geographic Encoding is complete, it will call the CLGeocodeCompletionHandler block to pass two parameters error: When the Encoding error (such as encoding does not produce specific information) has a value of placemarks: contains the CLPlacemark object. CLPlacemark Description: CLPlacemark literally indicates a landmark, which encapsulates detailed geographic location information @ property (nonatomic, readonly) CLLocation * location; region @ property (nonatomic, readonly) CLRegion * region; detailed address information @ property (nonatomic, readonly) NSDictionary * addressDictionary; address name @ property (nonatomic, readonly) NSString * name; city @ property (nonatomic, readonly) NSString * locality; 2. Sample Code: Build the interface in storyboard as follows: implementation code: YYViewController. m file copy code 1 // 2 // YYViewController. m 3 // 19-geocode 4 // 5 // Created by apple on 14-8-11. 6 // Copyright (c) 2014 yangyong. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 # import <CoreLocation/CoreLocation. h> 11 12 @ interface YYViewController () 13 @ property (nonatomic, strong) CLGeocoder * geocoder; 14 # pragma mark-geocode 15-(IBAction) geocode; 16 @ property (weak, nonatomic) IBOutlet UITextField * addressField; 17 @ property (weak, nonatomic) using UILabel * longitudeLabel; 18 @ property (weak, nonatomic) IBOutlet UILabel * latitudeLabel; 19 @ property (weak, nonatomic) IBOutlet UILabel * detailAddressLabel; 20 21 # pragma mark-anti-geocode 22 23-(IBAction) reverseGeocode; 24 @ property (weak, nonatomic) IBOutlet UITextField * longitudeField; 25 @ property (weak, nonatomic) IBOutlet UITextField * latitudeField; 26 @ property (weak, nonatomic) IBOutlet UILabel * reverdeDetailAddressLabel; 27 @ end 28 29 @ implementation YYViewController 30 31 # pragma mark-lazy loading 32-(CLGeocoder *) geocoder 33 {34 if (_ geocoder = nil) {35 _ geocoder = [[CLGeocoder alloc] init]; 36} 37 return _ geocoder; 38} 39-(void) viewDidLoad 40 {41 [super viewDidLoad]; 42} 43/** 44 * geographic code: Geographic Name-> latitude and longitude coordinates 45 */46-(IBAction) geocode {47 // 1. obtain the input address 48 NSString * address = self. addressField. text; 49 if (address. length = 0) return; 50 51 // 2. start geocoding 52 // Description: Call the following method to start encoding. Methods 53 [self. geocoder geocodeAddressString: address completionHandler: ^ (NSArray * placemarks, NSError * error) {54 // if there is an error message, or the number of place name elements retrieved in the array is 0, 55 if (error | placemarks. count = 0) {56 self. detailAddressLabel. text = @ "the address you entered cannot be found, it may be on the moon"; 57} else // code successful, specific location information found 58 {59 // print all location information found 60/* 61 name: name 62 locality: city 63 country: country 64 postalCode: zip code 65 */66 for (CLPlacemark * placemark in placemarks) {67 NSLog (@ "name = % @ locality = % @ country = % @ postalCode = % @", placemark. name, placemark. locality, placemark. country, placemark. postalCode); 68} 69 70 // The first in the retrieved geographic information array is displayed on the Interface 71 CLPlacemark * firstPlacemark = [placemarks firstObject]; 72 // detailed address name 73 self. detailAddressLabel. text = firstPlacemark. name; 74 // latitude 75 CLLocationDegrees latitude = firstPlacemark. location. coordinate. latitude; 76 // longitude 77 CLLocationDegrees longpolling = firstPlacemark. location. coordinate. longpolling; 78 self. latitudeLabel. text = [NSString stringWithFormat: @ "%. 2f ", latitude]; 79 self. longitudeLabel. text = [NSString stringWithFormat: @ "%. 2f ", longpolling]; 80} 81}]; 82} 83 84/** 85 * anti-geographic code: longitude and latitude coordinates-> Place Name 86 */87-(IBAction) reverseGeocode {88 // 1. obtain the input longitude and latitude 89 NSString * longtitudeText = self. longitudeField. text; 90 NSString * latitudeText = self. latitudeField. text; 91 if (longtitudeText. length = 0 | latitudeText. length = 0) return; 92 93 CLLocationDegrees latitude = [latitudeText doubleValue]; 94 CLLocationDegrees longpolling = [longtitudeText doubleValue]; 95 96 CLLocation * location = [[CLLocation alloc] initWithLatitude: latitude longpolling: longpolling]; 97 // 2. anti-geocode 98 [self. geocoder reverseGeocodeLocation: location completionHandler: ^ (NSArray * placemarks, NSError * error) {99 if (error | placemarks. count = 0) {100 self. reverdeDetailAddressLabel. text = @ "the address you entered cannot be found. It may be on the moon "; 101} else // code 102 {103 // display the front landmark information 104 CLPlacemark * firstPlacemark = [placemarks firstObject]; 105 self. reverdeDetailAddressLabel. text = firstPlacemark. name; 106 // latitude and longitude 107 CLLocationDegrees latitude = firstPlacemark. location. coordinate. latitude; 108 CLLocationDegrees longpolling = firstPlacemark. location. coordinate. longpolling; 109 self. latitudeField. text = [NSString stringWithFormat: @ "%. 2f ", latitude]; 110 self. longitudeField. text = [NSString stringWithFormat: @ "%. 2f ", longpolling]; 111} 112}]; 113} 114-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event116 {115 [self. view endEditing: YES]; 118} 119 @ end