IPhone development Note (13) Call GoogleMap API for geoinverse Encoding

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.

Https://github.com/mjisrawi/iOS-Geocoding-Services

Https://github.com/samvermette/SVGeocoder

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

Self. locationmanager = [[cllocationmanager alloc] init]; // create a location manager self. locationmanager. delegate = self; // sets proxy self. locationmanager. desiredaccuracy = kcllocationaccuracybest; // specify the required precision level self. locationmanager. distancefilter = 1000.0f; // set the distance filter [self. locationmanager startupdatinglocation]; // start location manager nsstring * STR = [nsstring stringwithformat: @ "http://maps.googleapis.com/maps/api/geocode/json? Latlng = % F, % F & sensor = trueion = 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 failed: @ "" 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.