获取带有纬度和经度的城市和国家名称 (iOS)

Get the city and country name with latitude and longitude (iOS)

我使用这个功能:

func getDataCity(tmpLat:Double,tmpLong:Double) {

    let tmpCLGeocoder = CLGeocoder.init()

        let tmpDataLoc = CLLocation.init(latitude: tmpLat, longitude: tmpLong)

        tmpCLGeocoder.reverseGeocodeLocation(tmpDataLoc, completionHandler: {(placemarks,error) in

            guard let tmpPlacemarks = placemarks else{
                return
            }
            let placeMark = tmpPlacemarks[0] as CLPlacemark

            // Country
            guard let countryLocality = placeMark.country else{
                return
            }

            // City
            guard let cityLocality = placeMark.locality else{
                return
            }


            print(countryLocality)
            print(cityLocality)         
        })     
}

当我使用坐标时,例如,从Berlin/Germany

getDataCity(tmpLat: 52.52000659999999, tmpLong: 13.404953999999975)

该功能运行良好,它显示了城市和国家/地区。但是我使用小城市或岛屿的坐标,例如杰尔巴岛(突尼斯的岛屿)

getDataCity(tmpLat: 33.8075978, tmpLong: 10.845146699999987)

该函数不打印任何内容。苹果有解释吗?我该怎么办?


要在您自己的项目中使用该函数,请将 CLLocationManagerDelegate 添加到 ViewController 并且不要忘记在您的 [=30= 中添加 Privacy - Location When In Use Usage Description ]

Apple 的地图不包含该位置的城市。您的代码确实可以识别国家/地区,但无法识别那里的城市(在这种情况下您会中止)。

如果您打开 Apple 地图,您会注意到该岛上没有标记任何城市,并且搜索突尼斯 Houmt Arbah(最近的城镇)不会 return Apple 地图中的结果(奇怪的是 returns Dahmani,这是一个大陆城镇;我不知道为什么)。它确实知道 Houmt Souq,但距离给定位置有很远的距离。

总而言之,Apple 的地图数据库对突尼斯的地理了解不多。如果您花一点时间环顾 Google 地图与 Apple 地图,您会发现 Apple 地图对突尼斯的几个地方知之甚少。例如,如果您在卫星模式下查看 Douz,然后切换到地图模式,您会看到 Apple 的卫星图像包括未绘制地图的整个村庄 (Al-Qalah)。而杜兹本身(一个拥有 38,000 人的小镇)的街道地图,坦率地说,是可悲的。

虽然 Apple 的地图多年来有了显着改进,并且在某些区域现在比 Google 的地图更好,但在大多数地方 Google 往往拥有更好的信息。如果您需要任意地点的最佳地图,Google 的地图是当今的黄金标准。