This article reprinted to http://blog.csdn.net/lvxiangan/article/details/28101119
The following excerpts from the network, the copyright belongs to the original author
Method 1: Using the iOS self-networking query feature, the Pbrequester failed with error error Domain=nsurlerrordomain code=-1009 "seems to have disconnected from the Internet. "userinfo=0x1e2ea840 {nserrorfailingurlstringkey=https://gsp4-cn.ls.apple.com/placesearchservlet, Nserrorfailingurlkey=https://gsp4-cn.ls.apple.com/placesearchservlet, nslocalizeddescription= seems to have disconnected from the Internet. , nsunderlyingerror=0x1e1172c0 "seems to have disconnected from the Internet. "}
Obtaining location information for the current location requires the use of a new class,Mkreversegeocoder. This class is in mapkit.framework. We add the framework and import the header file to use.
Hit a code, the results found that the class iOS5.0 is not necessary. Really In order to take care of compatibility, we first study Mkreversegeocoder, and then to study the new class, well, the name is Clgeocoder, well, not misspelled. inside the corelocation.
First say the usage of mkreversegeocoder. (Can't help coming out to spit a groove, generally see the red line that was crossed off, on the one side hit enter, still really can't say out of vainly disobey AH. )
。。。。。。。。。。。。。。。
Indicates that the test is complete, thenetwork of iOS is always suspended animation, a suspended animation, all kinds of functions need to use the network, it does not work. Sang-san-xin.
Mkreversegeocoder initialization, very simple.
Mkreversegeocoder *geocoder = [[Mkreversegeocoder alloc] initwithcoordinate:currentcoordinate2d];
Geocoder.delegate = self;
[Geocoder start];
After calling the above code, the reverse address encoding API is automatically invoked . We use proxies to receive this. As for the proxy method, we need to implement two.
-(void) Reversegeocoder: (Mkreversegeocoder *) Geocoder Didfindplacemark: (Mkplacemark *) Placemark {
}
-(void) Reversegeocoder: (Mkreversegeocoder *) Geocoder didfailwitherror: (Nserror *) error{
}
The first method is to obtain a reverse-coded. The second is when the reverse encoding fails, the processing error is used.
We mainly discuss the first method.
Placemark (anobject of the Mkplacemark Class) is actually a property of the Geocoder (the object of the Mkreversegeocoder class). From Geocoder inside take placemark this and direct take placemark This actually difference is not big. And the information we need is mostly in this.
This dictionary holds the underlying data
@property (nonatomic, readonly) nsdictionary *addressdictionary;
Let's try to get something out of this dictionary.
Here is the dictionary I poured out with this addressdictionary property. Let's look at the analysis.
{
City = "\U897F\U5B89\U5E02";//Urban name
Country = "\U4E2D\U56FD";//Country name
CountryCode = cn;//Country Code
Formattedaddresslines = (
"\U4E2D\U56FD",
"\U9655\U897F\U7701\U897F\U5B89\U5E02\U96C1\U5854\U533A",
"\u9ad8\u65b0\u516d\u8def34\u53f7"
); This is supposed to be the formatted address.
State = "\u9655\u897f\u7701"; Province
Street = "\u9ad8\u65b0\u516d\u8def 34\u53f7";//Street Full Name
sublocality = "\u96c1\u5854\u533a";//Zone name
Subthoroughfare = "34\U53F7";//Specific address
Thoroughfare = "\u9ad8\u65b0\u516d\u8def";//Street name
}
Note: The above dictionary is a dictionary that can be converted directly to a contact, via this abcreatestringwithaddressdictionary property.
The following are the other properties of Placemark. You can take it at your own discretion.
Address Dictionary Properties
@property (nonatomic, readonly) nsstring *name;//EG. Apple INC.
@property (nonatomic, readonly) nsstring *thoroughfare;//street address, eg. 1 Infinite Loop
@property (nonatomic, readonly) nsstring *subthoroughfare;//eg. 1
@property (nonatomic, readonly) nsstring *locality;//city, eg. Cupertino
@property (nonatomic, readonly) nsstring *sublocality;//neighborhood, common name, eg. Mission District
@property (nonatomic, readonly) nsstring *administrativearea;//state, eg. CA
@property (nonatomic, readonly) nsstring *subadministrativearea;//county, eg. Santa Clara
@property (nonatomic, readonly) nsstring *postalcode;//zip code, eg. 95014
@property (nonatomic, readonly) nsstring *isocountrycode;//EG. US
@property (nonatomic, readonly) nsstring *country;//EG. States
@property (nonatomic, readonly) nsstring *inlandwater;//EG. Lake Tahoe
@property (nonatomic, readonly) nsstring *ocean;//EG. Pacific Ocean
@property (nonatomic, readonly) Nsarray *areasofinterest;//EG. Golden Gate Park
Note: I found in the process that if the network is suspended from death, it may not be possible to get adverse results for a long time. This may require attention.
Ah ah ah, almost forget,IOS5 not recommend the use of a lot of my above mentioned. We need to use this Clgeocoder class.
The method of use is also very simple. Refer to the following steps:
First create a Clgeocoder object, and then call his -(void) Reversegeocodelocation: (cllocation *) Location Completionhandler: (clgeocodecompletionhandler) Completionhandler; method. The parameter types are passed as required. Some cheese will ask this clgeocodecompletionhandler how to write something? This is actually IOS4 after the official highly recommended BLOCK, will not use the classmate quickly go to see the document it.
This is the definition of Clgeocodecompletionhandler. typedef void(^clgeocodecompletionhandler) (nsarray *placemarks, nserror *error); All we need to do is write a block object and pass it in.
The following is a reference code that uses Clgeocoder. No agent, is it very happy?
clgeocoder* Geocoder = [[clgeocoder alloc] init];
[Geocoder reversegeocodelocation: NewLocation completionhandler:^ (nsarray* placemarks, nserror* error) {
NSLog(@ "%@", placemarks);
}];
Method 2: Use the Google Maps API
Find address based on latitude and longitude-(void) GetAddress: (NSString *) alongitude withlatitude: (NSString *) alatitude Withblock: (void (^) (id result )) Callback {callbackblock = callback; NSString *url = @ "Http://maps.googleapis.com/maps/api/geocode/json"; NSString *coordinatestr = [NSString stringwithformat:@ "%@,%@", Alatitude,alongitude]; Nsmutabledictionary *params = [[Nsmutabledictionary alloc] init]; [Params setvalue:@ "true" forkey:@ "sensor"]; [Params setvalue:@ "cn" forkey:@ "Language"]; [Params setobject:coordinatestr forkey:@ "latlng"]; NSString *tempparamstr = [self addparameterstorequest:params]; NSString *tempurl = [NSString stringwithformat:@ "%@?%@", url,tempparamstr]; Nsmutableurlrequest *operationrequest = [[Nsmutableurlrequest alloc] Initwithurl:[nsurl Urlwithstring:tempurl]]; Nsurlconnection *operationconnection = [[Nsurlconnection alloc] Initwithrequest:operationrequest delegate:self Startimmediately:no]; [Operationconnection start];} -(NSString *) addparameterstoreqUest: (nsmutabledictionary*) Parameters {Nsmutablearray *paramstringsarray = [Nsmutablearray arraywithcapacity:[[ Parameters AllKeys] [count]]; For (NSString *key in [parameters AllKeys]) {nsobject *paramvalue = [Parameters Valueforkey:key];if ([paramvalue] Kindofclass:[nsstring class]] {[Paramstringsarray addobject:[nsstring stringwithformat:@ "%@=%@", Key, [(NSString *) Paramvalue encodedurlparameterstring]];} else {[Paramstringsarray addobject:[nsstring stringwithformat:@ "%@=%@", Key, Paramvalue]];} } nsstring *paramsstring = [Paramstringsarray componentsjoinedbystring:@ "&"]; return paramsstring;}