-1
A
回答
26
這是很容易的。
let address = "1 Infinite Loop, Cupertino, CA 95014"
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks, error) in
guard
let placemarks = placemarks,
let location = placemarks.first?.location
else {
// handle no location found
return
}
// Use your location
}
您還需要添加和導入CoreLocation框架。
+0
我在哪裏使用「位置」變量? – Arcadian
5
您可以使用CLGeocoder,你可以轉換地址(字符串),以協調和你相反,試試這個:
import CoreLocation
var geocoder = CLGeocoder()
geocoder.geocodeAddressString("your address") {
placemarks, error in
let placemark = placemarks?.first
let lat = placemark?.location?.coordinate.latitude
let lon = placemark?.location?.coordinate.longitude
print("Lat: \(lat), Lon: \(lon)")
}
+0
我該如何返回一個CLLocation值? –
+0
placemark?.location是一個CLLocation類型。 – Greg
2
這工作
let geocoder = CLGeocoder()
let address = "8787 Snouffer School Rd, Montgomery Village, MD 20879"
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error ?? "")
}
if let placemark = placemarks?.first {
let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
print("Lat: \(coordinates.latitude) -- Long: \(coordinates.longitude)")
}
})
相關問題
- 1. 將地址轉換爲地理座標
- 2. 將地址轉換爲座標
- 3. 將座標轉換爲地址
- 4. 將GPS座標轉換爲Swift中的城市名稱/地址
- 5. 將UIKit座標轉換爲Quartz/CoreImage(Swift)
- 6. 將CLLocationCoordinate2D座標轉換爲CGPoint Swift
- 7. 將座標轉換爲地址的反向地理編碼?
- 8. 將地理座標列表轉換爲地址
- 9. 將地址轉換爲Python中的地理座標
- 10. python - 將座標數量轉換成地址或城市地址
- 11. 將捕獲的GPS座標轉換爲地址爲PHP
- 12. 如何將地址(作爲文本)轉換爲GPS座標?
- 13. 將本地(=旋轉後的)座標轉換爲全局座標
- 14. 將座標轉換爲地名
- 15. 如何將GPS座標轉換爲完整的地址與PHP?
- 16. 將街道地址轉換爲座標並放置在html中
- 17. 將街道地址轉換爲座標android
- 18. 將座標轉換爲地址v3 google api
- 19. 如何將gps座標轉換爲地址?
- 20. 轉換地址字段到座標
- 21. 將座標轉換爲像素座標
- 22. 將本地座標轉換爲全局座標
- 23. 將緯度經度座標轉換爲圖像地圖座標
- 24. 如何將本地對象座標轉換爲世界座標?
- 25. 將像素座標轉換爲地圖座標
- 26. 將瓦片座標轉換爲像素到地理座標
- 27. 公式將當地座標轉換爲世界座標
- 28. 從地理座標轉換爲地磁座標
- 29. iOS如何在2015年將地址轉換爲GPS座標(地理編碼)?
- 30. 轉換地圖座標到座標系
你真的搜查,沒有發現什麼?你有沒有閱讀Apple的CoreLocation文檔? https://developer.apple.com/reference/corelocation/clgeocoder –