如何从iPhone核心位置获取当前(美国)状态?

问题描述 投票:0回答:3

我希望我的 iOS 应用程序能够显示特定于用户当前所在州的信息(即加利福尼亚州、俄勒冈州等)。这是如何使用核心位置来完成的? CLLocation 类具有坐标 — 如何将其转换为状态? (仅供参考,我不需要更多细节,例如城市或街道)

iphone xcode geolocation core-location
3个回答
4
投票

3
投票

正如 Brad 提到的,您可以使用 Google(或 Mapquest 等)来获取反向地理编码信息。 Google 提供 XML、JSON 或 CSV 格式的数据。因此,您可以使用如下 URL:

http://maps.google.com/maps/geo?q=LATITUDE,LONGITUDE&output=csv&sensor=true

因此,http://maps.google.com/maps/geo?q=37.032978,-95.620834&output=csv&sensor=true 会将您带到堪萨斯州科菲维尔,您可以从那里获取信息。

FWIW,如果您使用MapKit,您还可以使用MKReverseGeocoder 来获取MKPlacemark。


0
投票

非常老的问题,但接受的答案实际上并没有回答问题。如果您想从 CoreLocation 获取状态,您可以像这样进行反向地理编码:

let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
    if let placemark = placemarks?.first, let state = placemark.administrativeArea, placemark.isoCountryCode == "US" {
        print("Current state = \(state)")
    } else {
        if placemarks?.first?.isoCountryCode != "US" {
            print("Not in the US")
        } else if let error {
            print("Did fail reverse geocode with error: \(error.localizedDescription)")
        } else {
            print("Couldn't find state when reverse geocoding")
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.