如何从CLLocationManager传递经度和纬度以使用全局swift

时间:2015-05-13 09:54:40

标签: swift cllocationmanager latitude-longitude

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        var locValue:CLLocationCoordinate2D = manager.location.coordinate
        println("locations = \(locValue.latitude) \(locValue.longitude)")


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

        if (error != nil) {
            println("Error: " + error.localizedDescription)
            return
        }

        if placemarks.count > 0 {
            let pm = placemarks[0] as CLPlacemark
            self.locationManager.stopUpdatingLocation()
            //self.dismissViewControllerAnimated(true, completion: nil)
            self.displayLocationInfo(pm)


        } else {
            println("Error with the data.")
        }


    })

}

目前,我可以通过坐标获取当前位置,并且我可以打印它。但是如何将函数中的值发送到Web服务?我发现变量在函数之外是不可访问的。有人可以帮忙吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

如果您想在CLLocationManagerDelegate方法之外访问您的位置变量,您应该在类定义开始的位置声明它:

class MyClass {
    private var currentCoordinate: CLLocationCoordinate2D?
}

然后在您的位置委托方法中指定当前值:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    currentCoordinate = manager.location.coordinate
}