Swift以其名称获取城市的坐标

时间:2015-04-21 14:55:41

标签: swift mapkit

我试图通过它的名字获得一个城市的纬度和经度。 如果我输入城市名称为"伦敦"我的输出应该是它的坐标

我可以使用以下代码通过坐标获取名称:

    var longitude :CLLocationDegrees = 41.0247
    var latitude :CLLocationDegrees = 40.522

    var location = CLLocation(latitude: latitude, longitude: longitude)
    println(location)



    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
        println(location)

        if error != nil {
            println("Reverse geocoder failed with error" + error.localizedDescription)
            return
        }

        if placemarks.count > 0 {
            let pm = placemarks[0] as! CLPlacemark
            println(pm.locality)
        }
        else
        {
            println("Problem with the data received from geocoder")
        }
    })

1 个答案:

答案 0 :(得分:2)

反向地理编码将坐标作为输入并输出地址/管理级别。

地理编码恰恰相反,这就是你所追求的。看看CLGeocoder's documentation page,它就在那里。