ios – Swift中的反向地理编码位置[已关闭]

我的输入是纬度和经度.我需要使用swift的reverseGeocodeLocation函数,给我本地的输出.我试图使用的代码是
println(geopoint.longitude) 
            println(geopoint.latitude)
            var manager : CLLocationManager!
            var longitude :CLLocationDegrees = geopoint.longitude
            var latitude :CLLocationDegrees = geopoint.latitude

            var location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude,longitude)
            println(location)

            CLGeocoder().reverseGeocodeLocation(manager.location,completionHandler: {(placemarks,error) -> Void in
                println(manager.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")
                }

在我得到的日志中

//-122.0312186
//37.33233141
//C.CLLocationCoordinate2D
//fatal error: unexpectedly found nil while unwrapping an Optional value

看来CLLocationCoordinate2DMake功能失败,这会导致reverseGeocodeLocation函数中的致命错误.我把这个格式弄糟了吗?

解决方法

你永远不会反转地理编码的位置,但你通过manager.location.

看到:
CLGeocoder().reverseGeocodeLocation(manager.location,…

我认为这是一个复制和粘贴错误,这是一个问题 – 代码本身看起来不错 – 几乎;)

工作代码

var longitude :CLLocationDegrees = -122.0312186
    var latitude :CLLocationDegrees = 37.33233141

    var location = CLLocation(latitude: latitude,longitude: longitude) //changed!!!
    println(location)

    CLGeocoder().reverseGeocodeLocation(location,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")
        }
    })

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...