2015-04-21 91 views
-1

我想通過名稱獲取城市的經度和緯度。 如果我輸入城市名稱爲「倫敦」我的輸出應該是它的座標Swift通過名稱獲取城市座標

我可以通過使用此代碼座標得到名稱:

var longitude :CLLocationDegrees = 41.0247 
    var latitude :CLLocationDegrees = 40.522 

    var location = CLLocation(latitude: latitude, longitude: longitude) 
    println(location) 



    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in 
     println(location) 

     if error != nil { 
      println("Reverse geocoder failed with error" + error.localizedDescription) 
      return 
     } 

     if placemarks.count > 0 { 
      let pm = placemarks[0] as! CLPlacemark 
      println(pm.locality) 
     } 
     else 
     { 
      println("Problem with the data received from geocoder") 
     } 
    }) 

回答

1

反向地理編碼採用座標輸入和輸出地址/行政級別。

地理編碼正好相反,這就是你所追求的。看看CLGeocoder's documentation page,它就在那裏。

相關問題