IPhone development Note (13) Call GoogleMap API to implement geoinverse editing

Source: Internet
Author: User

In iOS 5, Apple canceled the feature of georeverse encoding. I used this feature in my graduation project. I checked the Code in this regard, there are two good open-source libraries that can implement this function, but in the end they still call the GoogleMap API.

Both open-source libraries can implement functions well, but for the purpose of learning, I wrote a code to call the GoogleMap API and JSON parsing.

1. first introduce the code of JSON-framework and ASIHTTPRequest.

2. The following is the implementation code

[Plain]
Self. locationManager = [[CLLocationManager alloc] init]; // create a location manager
Self. locationManager. delegate = self; // sets the proxy.
Self. locationManager. desiredAccuracy = kCLLocationAccuracyBest; // specify the expected precision level.
Self. locationManager. distanceFilter = 1000.0f; // you can specify a distance filter.
[Self. locationManager startUpdatingLocation]; // start the Location Manager


NSString * str = [NSString stringWithFormat: @ "http://maps.googleapis.com/maps/api/geocode/json? Latlng = % f, % f & sensor = true®Ion = zh & language = zh-CN ", [[locationManager location] coordinate]. latitude, [[locationManager location] coordinate]. longpolling];

NSURL * url = [NSURL URLWithString: str];
ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
[Request startSynchronous];
NSString * response = [request responseString];
NSLog (@ "% @", response );
NSString * strrrr = [response stringByReplacingOccurrencesOfString: @ "\ n" withString: @ ""];
NSString * straaa = [strrrr stringByReplacingOccurrencesOfString: @ "" withString: @ ""];
Nsange range = NSMakeRange (11, [straaa length]-26 );
NSString * strbbb = [straaa substringWithRange: range];
NSMutableArray * dictionary = [strbbb JSONValue];
Self. addressStr = [[dictionary objectAtIndex: 0] objectForKey: @ "formatted_address"];

3. here we need to note that the JSON string returned by GoogleMap API contains many spaces and line breaks, which leads to errors during JSON parsing. Therefore, we need to process the returned string, replace spaces and line breaks. Because this string is very complex, I finally simply used the nsange to directly extract the part of the string I needed.


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.