The MapKit framework provides many useful features, except the return of 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, however, you can overcome the 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] to enter the string with the address you want to transform coordinates. The output returned is of type:
1 | 200,8,41.9128300,12.2241172 |
). The first value, 200 , indicates that everything went well ( 200 OK ). The second, 8 , is the Google accuracy parameter (1-10). The last two values are, finally, latitude and longitude. Now we see a prototype of a method can 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; } |
Notes of Interest
, alla stregua della funzione explode ( ) del PHP per intenderci. The string returned in locationString is "splitted" by the method componentsSeparatedByString , like the function explode ( ) of PHP for instance. In the example I proposed I entered - but commented - the code to retrieve even the Google parameter accuracy, precision or scale factor, denoted by zoom .
Source as
For completeness, I made a small example application with which you can try the method proposed above, enter any address and the iPhone will display on the map.
I thank the team devAPP for the inspiration of this article.










Latest Comments
Mark : Thank you very much, I've lit
I solved it by setting [cc_objc] / / OptionViewController.m - ...
Giovambattista Fazioli : @ Mark: I suggest you think a more correct approach. If you run the subclass of the tab ...
Mark : Excuse the spam .. I noticed that there is an error .. here is the correction [cc_objc] / PrimaClasse.h ** ** / # import ...
Marco : forgotten .. in [cci] OptionViewController [/ cci] for [cci] @ syntetize [/ cci] I put the delegate
louis : very clear and simple I have to admit that writing a pa hardly use delegates created by ...