【问题标题】:How to convert address into coordinates to place on a map in SwiftUI? [closed]如何将地址转换为坐标以放置在 SwiftUI 中的地图上? [关闭]
【发布时间】:2020-12-18 20:55:49
【问题描述】:

我将如何获取字符串地址,例如从 json 文件中获取。 “23 Duncan St, New York City, NY”并将其转换为 Swift 的纬度/经度坐标,然后用于放置在地图上?

这感觉应该是简单流行的,但我找不到解决办法。

【问题讨论】:

标签: json swift swiftui


【解决方案1】:

使用 CoreLocation 框架中的 CLGeocoder

let address = "23 Duncan St, New York City, NY"
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks, error) in
    guard
        let placemarks = placemarks,
        let location = placemarks.first?.location
    else { return }
    // do whatever you want with CLLocaiton object
}

并且坐标直接来自CLLocation对象。

【讨论】:

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