The MapKit framework provides a lot of useful features, except the return Latitude and Longitude from an address. In JavaScript, for example, you can use the service provided by Google Geocoding and discussed in Google Maps: how to get Latitude and Longitude from an address . On Apple iPhone, or iPad, you can however overcome this obstacle by using a different Google services. Specifically, you can directly call the url:
1 | http://maps.google.com/maps/geo?q = [address] & output = csv |
Where is [indirizzo] enter the string with the address which we want to transform coordinates. The output returned is of the type:
1 | 200,8,41.9128300,12.2241172 |
). The first value, 200 , indicates that all is well ( 200 OK ). The second, 8 , is the Google accuracy parameter (1-10). The last two values are, finally, latitude and longitude. Let us now see a possible prototype of a method to be included in our applications:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | CLLocationCoordinate2D ) getLocationFromAddress : ( NSString * ) address { - (CLLocationCoordinate2D) getLocationFromAddress: ( NSString *) address { urlString = [ NSString stringWithFormat : @ "http://maps.google.com/maps/geo?q=%@&output=csv" , NSString * urlString = [ NSString stringWithFormat: @ "http://maps.google.com/maps/geo?q =% @ & output = csv", NSUTF8StringEncoding ] ] ; [Address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; listItems = [ locationString componentsSeparatedByString : @ "," ] ; NSArray * listItems = [locationString componentsSeparatedByString: @ ","]; / / Int zoom = 0; 0.0 ; double latitude = 0.0; 0.0 ; double longitude = 0.0; listItems count ] > = 4 && [ [ listItems objectAtIndex : 0 ] isEqualToString : @ "200" ] ) { if ([listItems count]> = 4 && [[listItems objectAtIndex: 0] isEqualToString: @ "200"]) { / / Zoom = [[listItems objectAtIndex: 1] intValue]; listItems objectAtIndex : 2 ] doubleValue ] ; latitude = [[listItems objectAtIndex: 2] doubleValue]; listItems objectAtIndex : 3 ] doubleValue ] ; longitude = [[listItems objectAtIndex: 3] doubleValue]; { Else {} / / Error } CLLocationCoordinate2D location; location.latitude = latitude; location.longitude = longitude; return location; } |
Interesting notes
, alla stregua della funzione explode ( ) del PHP per intenderci. The string returned in locationString is "split" by the method componentsSeparatedByString , like the function explode ( ) php to speak. In the example I proposed I entered - but commented - also the code to retrieve the Google parameter accuracy, precision or scale factor, denoted zoom .
Source eg
For completeness, I created a small sample application with which you can try the method proposed above, enter any address and iPhone will show on the map.
I thank the team devAPP for the inspiration of this article.













How do I get the inverse? I have a position expressed in latitude, longitude, and I want to know the address, city and state.
Thanks
@ Mirko: see Objective-C: Getting information from Latitude and Longitude
Hello Giovan Battista,
your tutorial I was very helpful, but I need a little hint.
If I wanted to put an address Fixed, without always having to type in the search field, how can I do?
thanks
Daniela