Iphone reverse geographic resolution obtains the user's location information from coordinates
1. First, implement the MKReverseGeocoderDelegate delegate.
2. The Code is as follows:
// Request reverse geographic location resolution <br/> CLLocationCoordinate2D coordinate; <br/> coordinate. latitude = 122.004; <br/> coordinate. longpolling = 34.115; </p> <p> MKReverseGeocoder * geocoder = [[MKReverseGeocoder alloc] initWithCoordinate: coordinate]; <br/> geocoder. delegate = self; </p> <p> [geocoder start];
If the geographic information is parsed, the following code is called:
// Obtain the reverse geographic location of the current user <br/>-(void) reverseGeocoder :( MKReverseGeocoder *) geocoder didFindPlacemark :( MKPlacemark *) placemark <br/>{< br/> NSString * sss = [placemark. addressDictionary objectForKey: @ "FormattedAddressLines"]; <br/> if (sss) <br/>{< br/> NSLog (@ "sss = % @", [placemark. addressDictionary objectForKey: @ "FormattedAddressLines"]); <br/>}</p> <p> NSLog (@ "countryCode = % @", placemark. countryCode); <br/> NSLog (@ "Country = % @", placemark. country); <br/> NSLog (@ "administrativeArea = % @", placemark. administrativeArea); <br/> NSLog (@ "subAdministrativeArea = % @", placemark. subAdministrativeArea); <br/> NSLog (@ "locality = % @", placemark. locality); <br/> NSLog (@ "subLocality = % @", placemark. subLocality); <br/> NSLog (@ "thoroughfare = % @", placemark. thoroughfare); <br/> NSLog (@ "subThoroughfare = % @", placemark. subThoroughfare); <br/> NSLog (@ "PostalCode = % @", placemark. postalCode); </p> <p> NSString * addressResult = @ ""; <br/> NSString * beijin = @ "Beijing "; <br/> NSString * shanghai = @ "shanghai City"; <br/> NSString * tianjin = @ "tianjin City"; <br/> NSString * chongqin = @ "Chongqing City "; <br/> if ([placemark. administrativeArea isinclutostring: beijin] | <br/> [placemark. administrativeArea isw.tostring: shanghai] | <br/> [placemark. administrativeArea isw.tostring: tianjin] | <Br/> [placemark. administrativeArea isw.tostring: chongqin] <br/>) <br/>{< br/> addressResult = placemark. locality; <br/>}< br/> else <br/> {<br/> // if it is not China, add the country name <br/> if (placemark. countryCode! = @ "CN") <br/>{< br/> addressResult = [addressResult stringByAppendingString: placemark. country]; <br/> addressResult = [addressResult stringByAppendingString: @ ""]; <br/>}</p> <p> if ([placemark. administrativeArea length]> 0) <br/> {<br/> addressResult = [addressResult stringByAppendingString: @ ""]; <br/> addressResult = [addressResult stringByAppendingString: placemark. administrativeArea]; </p> <p> // if it is not in China Add spaces to each segment <br/> if (placemark. countryCode! = @ "CN") <br/>{< br/> addressResult = [addressResult stringByAppendingString: @ ""]; <br/>}</p> <p> if ([placemark. locality length]> 0) <br/> {<br/> addressResult = [addressResult stringByAppendingString: placemark. locality]; </p> <p> // if it is not China, spaces are directly added to each segment. <br/> if (placemark. countryCode! = @ "CN") <br/>{< br/> addressResult = [addressResult stringByAppendingString: @ ""]; <br/>}</p> <p> if ([placemark. subLocality length]> 0) <br/> {<br/> addressResult = [addressResult stringByAppendingString: placemark. subLocality]; </p> <p> // if not, add spaces to each segment. <br/> if (placemark. countryCode! = @ "CN") <br/>{< br/> addressResult = [addressResult stringByAppendingString: @ ""]; <br/>}</p> <p> if ([placemark. thoroughfare length]> 0) <br/> {<br/> addressResult = [addressResult stringByAppendingString: placemark. thoroughfare]; </p> <p> // if not, spaces are directly added to each segment. <br/> if (placemark. countryCode! = @ "CN") <br/>{< br/> addressResult = [addressResult stringByAppendingString: @ ""]; <br/>}</p> <p> if ([placemark. subThoroughfare length]> 0) <br/> {<br/> addressResult = [addressResult stringByAppendingString: @ ""]; <br/> addressResult = [addressResult stringByAppendingString: placemark. subThoroughfare]; <br/>}</p> <p> NSLog (@ "addressResult = % @", addressResult); <br/>}
If the parsing fails, the following function will be called:
// Obtain the reverse geolocation error of the current user <br/>-(void) reverseGeocoder :( MKReverseGeocoder *) geocoder didFailWithError :( NSError *) error <br/>{< br/> NSLog (@ "ReverseGeocoder error % @", [error description]); </p> <p> [geocoder release]; <br/>}