2013-09-01 19 views
7

Ho uno MKMapView che ha uno UISearchBar in alto e voglio che l'utente sia in grado di digitare un indirizzo, e di trovare quell'indirizzo e rilasciare un pin su di esso. Quello che non so è come trasformare la stringa di indirizzo in longitudine e latitudine, così posso creare un oggetto CLLocation. Qualcuno sa come posso fare questo?Come ottenere le coordinate lat e long dalla stringa di indirizzo

+0

Questo non aiuta, perché i dati che recupera ha le coordinate per gli indirizzi. Ho bisogno di un modo per trovare le coordinate per l'indirizzo, quando l'utente cerca l'indirizzo. –

risposta

16

È possibile trovare la risposta in questa domanda.

iOS - MKMapView place annotation by using address instead of lat/long Per utente Romes.

NSString *location = @"some address, state, and zip"; 
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
     [geocoder geocodeAddressString:location 
      completionHandler:^(NSArray* placemarks, NSError* error){ 
       if (placemarks && placemarks.count > 0) { 
        CLPlacemark *topResult = [placemarks objectAtIndex:0]; 
        MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult]; 

        MKCoordinateRegion region = self.mapView.region; 
        region.center = placemark.region.center; 
        region.span.longitudeDelta /= 8.0; 
        region.span.latitudeDelta /= 8.0; 

        [self.mapView setRegion:region animated:YES]; 
        [self.mapView addAnnotation:placemark]; 
       } 
      } 
     ]; 

A Soluzione molto semplice. Ma applicabile solo su iOS5.1 o versioni successive.

1

Per swift2

var location: String = "some address, state, and zip" 
     var geocoder: CLGeocoder = CLGeocoder() 
     geocoder.geocodeAddressString(location,completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in 
      if (placemarks?.count > 0) { 
       var topResult: CLPlacemark = (placemarks?[0])! 
       var placemark: MKPlacemark = MKPlacemark(placemark: topResult) 
       var region: MKCoordinateRegion = self.mapView.region 
       region.center = placemark.region.center 
       region.span.longitudeDelta /= 8.0 
       region.span.latitudeDelta /= 8.0 
       self.mapView.setRegion(region, animated: true) 
       self.mapView.addAnnotation(placemark) 
      } 
     }) 
2

ho usato un approccio simile come Vijay, ma ha dovuto modificare una riga di codice. region.center = placemark.region.center non ha funzionato per me. Forse il mio codice aiuta qualcuno così:

let location: String = "1 Infinite Loop, CA, USA" 
let geocoder: CLGeocoder = CLGeocoder() 
geocoder.geocodeAddressString(location,completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in 
    if (placemarks?.count > 0) { 
     let topResult: CLPlacemark = (placemarks?[0])! 
     let placemark: MKPlacemark = MKPlacemark(placemark: topResult) 
     var region: MKCoordinateRegion = self.mapView.region 

     region.center.latitude = (placemark.location?.coordinate.latitude)! 
     region.center.longitude = (placemark.location?.coordinate.longitude)! 

     region.span = MKCoordinateSpanMake(0.5, 0.5) 

     self.mapView.setRegion(region, animated: true) 
     self.mapView.addAnnotation(placemark) 
    } 
}) 
0
func geoCodeUsingAddress(address: NSString) -> CLLocationCoordinate2D { 
    var latitude: Double = 0 
    var longitude: Double = 0 
    let addressstr : NSString = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=\(address)" as NSString 
    let urlStr = addressstr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 
    let searchURL: NSURL = NSURL(string: urlStr! as String)! 
    do { 
     let newdata = try Data(contentsOf: searchURL as URL) 
     if let responseDictionary = try JSONSerialization.jsonObject(with: newdata, options: []) as? NSDictionary { 
      print(responseDictionary) 
      let array = responseDictionary.object(forKey: "results") as! NSArray 
      let dic = array[0] as! NSDictionary 
      let locationDic = (dic.object(forKey: "geometry") as! NSDictionary).object(forKey: "location") as! NSDictionary 
      latitude = locationDic.object(forKey: "lat") as! Double 
      longitude = locationDic.object(forKey: "lng") as! Double 
     } catch { 
     } 
    } 
    var center = CLLocationCoordinate2D() 
    center.latitude = latitude 
    center.longitude = longitude 
    return center 
} 
+0

questo è per google map (risposta in Latest swift 3) –