【问题标题】:Convert address to coordinates swift快速将地址转换为坐标
【发布时间】:2017-07-05 21:03:13
【问题描述】:

如何使用 Swift 将 String 地址转换为 CLLocation 坐标?我还没有代码,我正在寻找解决方案,但我还没有找到任何人。

谢谢。

【问题讨论】:

  • 你真的搜索了却一无所获?您是否费心阅读 Apple 的 CoreLocation 文档? developer.apple.com/reference/corelocation/clgeocoder
  • 我不推荐 CoreLocation 除非你想在你的应用程序中谨慎使用它,因为有一个非常紧张的配额,比如每分钟 1 个请求。
  • @bibscy 嗨,还有其他解决方案吗?
  • @PaulBénéteau 我正在使用 Google 地理编码 API。如果您发布问题并提供链接,我可以帮助您解答
  • @bibscy 使用 google api 您无法管理位置设置(精度、速率、过滤器...)

标签: ios swift coordinates core-location


【解决方案1】:

这很容易。

    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 框架。

【讨论】:

  • 我在哪里使用“位置”变量?
【解决方案2】:

您可以使用 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)")
}

【讨论】:

  • 如何返回 CLLocation 值?
【解决方案3】:

这是我想出的返回 CLLocationCoordinat2D 对象的方法:

func getLocation(from address: String, completion: @escaping (_ location: CLLocationCoordinate2D?)-> Void) {
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(address) { (placemarks, error) in
        guard let placemarks = placemarks,
        let location = placemarks.first?.location?.coordinate else {
            completion(nil)
            return
        }
        completion(location)
    }
}

假设我有这个地址:

let address = "Springfield, Illinois"

用法

getLocation(from: address) { location in
    print("Location is", location.debugDescription)
    // Location is Optional(__C.CLLocationCoordinate2D(latitude: 39.799372, longitude: -89.644458))

}

【讨论】:

  • 您应该始终调用完成处理程序。如果没有找到位置,请致电completion(nil)
  • @LinusGeffarth 感谢您更新我的答案。 :)
【解决方案4】:

这行得通

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

【讨论】:

    【解决方案5】:

    Swift 5 和 Swift 5.1

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多