2017-06-12 2 views
0
func getAddressFromLatLon() { 



    var locManager = CLLocationManager() 
    locManager.requestWhenInUseAuthorization() 
    var currentLocation = CLLocation() 

    if(CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || 
     CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorized){ 

     currentLocation = locManager.location! 

    } 




    var center : CLLocationCoordinate2D = CLLocationCoordinate2D() 
    let lat: Double = Double(currentLocation.coordinate.latitude) 
    //21.228124 
    let lon: Double = Double(currentLocation.coordinate.longitude) 
    //72.833770 
    let ceo: CLGeocoder = CLGeocoder() 
    center.latitude = lat 
    center.longitude = lon 

    let geoCoder = CLGeocoder() 
    var placemark: AnyObject 
    var error: NSError 
    geoCoder.reverseGeocodeLocation(locManager.location!, completionHandler: { (placemark, error) -> Void in 
     if error != nil { 
      print("Error: \(error?.localizedDescription)") 
      return 
     } 
     if (placemark?.count)! > 0 { 
      let pm = (placemark?[0])! as CLPlacemark 
      //self.addressString = pm.locality! + pm.country! 
      let adress = pm.locality! + " " + pm.country! 

      print(adress) 
     } else { 
      print("Error with data") 
     } 
    }) 



} 

Entschuldigen Sie diese grundlegende Frage. Ich bin relativ neu zu schnell und ich versuche, geocode eine geographische Breite und Länge umzukehren. Ich versuche, die Adresse von der umgekehrten Geocodierung zurückzugeben, aber es scheint zu Null zurückzukehren. Die fragliche Funktion ist getAddressFromLatLon() und ich habe versucht, einen Rückgabetyp hinzuzufügen, aber es gibt Nil zurück. Wenn ich in der Funktion selbst drucke, wird der korrekte Wert gedruckt, aber aus irgendeinem Grund habe ich Schwierigkeiten, die Adresse zurückzugeben, damit ich sie an andere Klassen/Funktionen weitergeben kann.Rückgabetyp Nill

+0

func getAddressFromLatLon() ist eine Leere Funktion. Sie können nicht erwarten, dass es irgendeinen Wert zurückgibt. – Developer

+0

Noch eine Sache, die ich bestätigen möchte, ist Print (Adresse) zeigt das gewünschte Ergebnis? .. – Developer

+0

hast du lat lange in dieser Funktion? – KKRocks

Antwort

0

Sie müssen den Wert behalten, den Sie vom Reserver-Geocodierer erhalten und nur verwenden müssen.

geoCoder.reverseGeocodeLocation(locManager.location!, completionHandler: { (placemark, error) -> Void in 
      if error != nil { 
       print("Error: \(error?.localizedDescription)") 
       return 
      } 
      if (placemark?.count)! > 0 { 
       let pm = (placemark?[0])! as CLPlacemark 
       //self.addressString = pm.locality! + pm.country! 
       let adress = pm.locality! + " " + pm.country! 

       // Store value into global object and you can use it... 
       // Another option, you can call one function and do necessary steps in it 
       mainLocality = pm.locality 
       mainCountry - pm.country  
       updateGeoLocation() // Call funcation 

       print(adress) 
} else { 
      print("Error with data") 
     } 
    }) 


func updateGeoLocation(){ 
    // You can use both object at here 
    // mainLocality 
    // mainCountry 
    // Do your stuff 

} 
0

Sie müssen CLLocation Objekt in ViewDidLoad initilise und Lage coordinatites darin delegieren Methode

Declate Variable für aktuellen Standort

var currentLocation : CLLocation 

In ViewDidLoad

var locManager = CLLocationManager() 

    locationManager.delegate = self; 
      locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
      // ask permission - NOT NECESSARY IF YOU ALREADY ADDED NSLocationAlwaysUsageDescription IT UP INFO.PLIST 
      locationManager.requestAlwaysAuthorization() 
      // when in use foreground 
      locationManager.requestWhenInUseAuthorization() 
      locationManager.startUpdatingLocation() 
erhalten

CLLocation den Delegierten

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     // Get first location item returned from locations array 
     currentLocation = locations[0] 
     self.getAddressFromLatLon() // you can call here or whenever you want 
    } 

Ihre Methode wie unten

func getAddressFromLatLon() { 

    var center : CLLocationCoordinate2D = CLLocationCoordinate2D() 
    let lat: Double = Double(currentLocation.coordinate.latitude) 
    //21.228124 
    let lon: Double = Double(currentLocation.coordinate.longitude) 
    //72.833770 
    let ceo: CLGeocoder = CLGeocoder() 
    center.latitude = lat 
    center.longitude = lon 

    let geoCoder = CLGeocoder() 
    var placemark: AnyObject 
    var error: NSError 
    geoCoder.reverseGeocodeLocation(locManager.location!, completionHandler: { (placemark, error) -> Void in 
     if error != nil { 
      print("Error: \(error?.localizedDescription)") 
      return 
     } 
     if (placemark?.count)! > 0 { 
      let pm = (placemark?[0])! as CLPlacemark 
      //self.addressString = pm.locality! + pm.country! 
      let adress = pm.locality! + " " + pm.country! 

      print(adress) 
     } else { 
      print("Error with data") 
     } 
    }) 



}