2014-10-06 17 views
5

Ho una posizione di latitudine/longitudine che voglio convertire nel nome della posizione String in Swift. Qual è il modo migliore per farlo? Credo che sia meglio usare la funzione reverseGeocodeLocation ma non esattamente come. Questo è quello che ho finora:Converti coordinate GPS in nome/indirizzo di una città in Swift

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
    if (locationFixAchieved == false) { 
     locationFixAchieved = true 
     var locationArray = locations as NSArray 
     var locationObj = locationArray.lastObject as CLLocation 
     var coord = locationObj.coordinate 
     var latitude = coord.latitude 
     var longitude = coord.longitude 

     getCurrentWeatherData("\(longitude)", latitude: "\(latitude)") 
     reverseGeocodeLocation(location:coord, completionHandler: { (<#[AnyObject]!#>, <#NSError!#>) -> Void in 
      <#code#> 
     }) 

    } 
} 

func reverseGeocodeLocation(location: CLLocation!, completionHandler: CLGeocodeCompletionHandler!){ 

} 
+0

attaccato quello che ho tentato. Penso di dover usare la funzione reverseGeocodeLocation ma non sono sicuro di come aggiungere –

risposta

11

è necessario scrivere il codice come questo:

geocoder.reverseGeocodeLocation(currentLocation, completionHandler: { 
      placemarks, error in 

       if error == nil && placemarks.count > 0 { 
        self.placeMark = placemarks.last as? CLPlacemark 
        self.adressLabel.text = "\(self.placeMark!.thoroughfare)\n\(self.placeMark!.postalCode) \(self.placeMark!.locality)\n\(self.placeMark!.country)" 
        self.manager.stopUpdatingLocation() 
       } 
      }) 
+1

aggiungendo che è necessario aggiungere tutti i componenti del segnaposto, basta usare: self.adressLabel.text = ABCreateStringWithAddressDictionary (self.placemark.addressDictionary !, false) per <= iOS 8 dal framework AdressBookUI e CNPostaAddressFormatter dal framework Contatti per> iOS 9.0 –

+0

Ah grazie, non lo sapevo! – Ben

4

si prega di controllare questa risposta.

func getAddressFromGeocodeCoordinate(coordinate: CLLocationCoordinate2D) { 
    let geocoder = GMSGeocoder() 
    geocoder.reverseGeocodeCoordinate(coordinate) { response , error in 

     //Add this line 
     if let address = response!.firstResult() { 
     let lines = address.lines! as [String] 
     print(lines) 

     } 
    } 
    } 
0

Swift codice per ottenere l'indirizzo del segnaposto, ben formattato

let address = ABCreateStringWithAddressDictionary(placemark.addressDictionary!, false).componentsSeparatedByString("\n").joinWithSeparator(", ") 

print(address!) 
3

Sto usando Swift 3/XCode 8

Ho usato Benjamin Herzog's answer ma mi sono imbattuto in alcuni errori di compilazione con a facoltativo e casting.

In riscrittura, ho deciso di incapsularlo in una funzione e generalizzarlo in modo che possa essere facilmente inserito ovunque.

import CoreLocation 

func getPlacemark(forLocation location: CLLocation, completionHandler: @escaping (CLPlacemark?, String?) ->()) { 
    let geocoder = CLGeocoder() 

    geocoder.reverseGeocodeLocation(location, completionHandler: { 
     placemarks, error in 

     if let err = error { 
      completionHandler(nil, err.localizedDescription) 
     } else if let placemarkArray = placemarks { 
      if let placemark = placemarkArray.first { 
       completionHandler(placemark, nil) 
      } else { 
       completionHandler(nil, "Placemark was nil") 
      } 
     } else { 
      completionHandler(nil, "Unknown error") 
     } 
    }) 

} 

Usandolo:

getPlacemark(forLocation: originLocation) { 
    (originPlacemark, error) in 
     if let err = error { 
      print(err) 
     } else if let placemark = originPlacemark { 
      // Do something with the placemark 
     } 
    }) 
} 
+0

Buon uomo !! Saluti –