【问题标题】:Unable to get city name by current latitude and longitude in swift无法通过当前经纬度快速获取城市名称
【发布时间】:2018-08-22 21:11:13
【问题描述】:

我正在尝试使用 CLGeocoder().reverseGeocodeLocation 从我当前的位置坐标中获取城市名称。

它给了我国家名称、街道名称、州名和许多其他信息,但没有城市。我的代码有什么问题吗?

这是我的代码:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]
    CLGeocoder().reverseGeocodeLocation(location) { (placeMark, error) in
        if error != nil{
            print("Some errors: \(String(describing: error?.localizedDescription))")
        }else{
            if let place = placeMark?[0]{
                print("country: \(place.administrativeArea)")

                self.lblCurrentLocation.text = place.administrativeArea
            }
        }
    } }

我也使用下面的代码。但对我不起作用。这是另一种方式。

        let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!)
    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

        // Place details
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]

        // Address dictionary
        print(placeMark.addressDictionary as Any)

        // Location name
        if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
            print("locationName: \(locationName)")
        }
        // Street address
        if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
            print("street: \(street)")
        }
        // City
        if let city = placeMark.addressDictionary!["City"] as? NSString {
            print("city : \(city)")
        }
        // Zip code
        if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
            print("zip :\(zip)")
        }
        // Country
        if let country = placeMark.addressDictionary!["Country"] as? NSString {
            print("country :\(country)")
        }
    })

请有人帮我获取城市名称。

【问题讨论】:

  • print(placeMark.addressDictionary as Any)这行的输出难道没有提示吗?
  • 你用的是哪个经纬度?
  • (self.locationManager.location?.coordinate.latitude)!, (self.locationManager.location?.coordinate.longitude)!我使用了当前位置的纬度和经度。是的,下拉提示提供了许多选项,但没有一个提供城市名称。

标签: ios swift clgeocoder currentlocation


【解决方案1】:

该字段称为locality

 if let locality = placeMark.addressDictionary!["locality"] as? NSString {
            print("locality :\(locality)")
        }

本地 Apple 文档

https://developer.apple.com/documentation/corelocation/clplacemark/1423507-locality?language=objc

CLPplacemark

https://developer.apple.com/documentation/corelocation/clplacemark?language=objc

更新:

试试这个

import Foundation
import CoreLocation

let geoCoder = CLGeocoder()
let location = CLLocation(latitude: 40.730610, longitude:  -73.935242) // <- New York

geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, _) -> Void in

    placemarks?.forEach { (placemark) in

        if let city = placemark.locality { print(city) } // Prints "New York"
    }
})

【讨论】:

  • 它不执行打印行。上面的代码是打印街道、国家、位置名称而不是城市。我也试过 ["Locality"] 和 ["locality"]。
  • 它在上述坐标上打印“纽约”,但在尼泊尔的坐标上不起作用。
  • 表示这个位置在苹果的注册上是不完整的。但这个领域是我告诉过的。在这种情况下,没有什么可做的。
猜你喜欢
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 2014-04-14
  • 2013-03-03
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多