简体   繁体   中英

Open iOS map with string address in Swift 2.0

I have the address in string format (not latitude or longitude) and it is not feasible to separate from the string the zip, city or road name.

Is there any possibility to open the map with the string? The string is formatted like this: Fussballplatz Talgut, Grüzefeldstrasse , 8400 Winterthur

I tried two examples: 1) http://www.techotopia.com/index.php/An_Example_Swift_iOS_8_MKMapItem_Application this has problem with kABPersonAddressStreetKey which is undefined in swift 2.0 (think so)

2) How to open maps App programmatically with coordinates in swift? here I do not have the latitude o longitude.

I think you should use CLGeocoder class.

This is the example from apple docs : https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html :

    /*
     Use the CLGeocoder class with a dictionary of Address Book
     information or a simple string to initiate forward-geocoding          
     requests. There is no designated format for string-based 
     requests: Delimiter characters are welcome, but not required, 
     and the geocoder server treats the string as case-insensitive. 
     For example, any of the following strings would yield results:

    "Apple Inc”
    "1 Infinite Loop”
    "1 Infinite Loop, Cupertino, CA USA” 
   */
[geocoder geocodeAddressString:@"1 Infinite Loop"
     completionHandler:^(NSArray* placemarks, NSError* error){
         for (CLPlacemark* aPlacemark in placemarks)
         {
             // Process the placemark.
         }
}];

take a look on this question:

Get latitude and longitude based on Address using Geocoder class in iOS

it has a pretty detailed answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM