Iphone reverse geographic resolution obtains the user's location information from coordinates

Source: Internet
Author: User

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/>} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.