2017-06-12 83 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") 
     } 
    }) 



} 

對不起,對於這個非常基本的問題。我對swift很陌生,我試圖將地理代碼反轉爲經緯度。我試圖從反向地理編碼返回地址,但它似乎返回零。有問題的函數是getAddressFromLatLon(),我嘗試添加一個返回類型,但它返回零。當我在函數本身中打印時,會打印正確的值,但由於某種原因,我無法使地址返回,因此我可以將其傳遞給其他類/函數。退貨方式Nill

+0

FUNC getAddressFromLatLon()方法是一個無效功能。你不能指望它返回任何價值。 – Developer

+0

還有一件事我想確認,是打印(地址)顯示所需的結果?.. – Developer

+0

你在這個功能拉長嗎? – KKRocks

回答

0

您需要保留從地理編碼器獲得的值,並且只需要使用它。

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

您需要initilise CLLocation對象viewDidLoad中,並在其中獲得位置coordinatites委託方法

Declate變量的當前位置

var currentLocation : CLLocation 

在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() 

CLLocation的代表

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 
    } 

你如下

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") 
     } 
    }) 



}