简体   繁体   English

Swift:如何使用CLLocationCoordinate2D将坐标存储为字符串或int?

[英]Swift: How can I store coordinates using CLLocationCoordinate2D as a string or an int?

So far, this is the code I have. 到目前为止,这是我的代码。 I'm trying to store the values of locValue.latitude and locValue.longitude as either a string or an integer. 我正在尝试将locValue.latitude和locValue.longitude的值存储为字符串或整数。 I've tried defining the return type in the method ( -> String, etc.) but I get an error saying the didChangeAuthorizationStatus method conflicts with optional requirement method locationManager in protocol CLLocationManagerDelegate. 我已经尝试在方法中定义返回类型( - > String等),但是我得到一个错误,说明didChangeAuthorizationStatus方法与协议CLLocationManagerDelegate中的可选需求方法locationManager冲突。

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .AuthorizedAlways {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        self.dispLocation.text = "locations = \(locValue.latitude) \(locValue.longitude)"
    }
}

Just declare two strings 只需声明两个字符串

var lat  = ""
var long = ""

And the from your location parse lat and long 并且从您的位置解析latlong

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location:CLLocationCoordinate2D = manager.location!.coordinate
    lat = String(location.latitude)
    long = String(location.longitude)
}

you can also try this - 你也可以尝试这个 -

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    var locValue:CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0);

    if manager.location?.coordinate != nil {
        locValue = (manager.location?.coordinate)!
    }

    let locations = "locations = \(locValue.latitude) \(locValue.longitude)"
    print(locations)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何存储CLLocationCoordinate2D? - How to store CLLocationCoordinate2D? NSUserDefaults并使用setobject方法在Swift中存储CLLocationCoordinate2D错误 - NSUserDefaults and using setobject method to store CLLocationCoordinate2D error in Swift 将 CLLocationCoordinate2D 转换为可存储的 String - Converting CLLocationCoordinate2D to a String that can be stored 如何检查CLLocationCoordinate2D是否位于四个CLLocationCoordinate2D Square内?在Objective C中使用Google Maps - How can I check if a CLLocationCoordinate2D is inside four CLLocationCoordinate2D Square? in Objective C with Google Maps 将CLLocationCoordinate2D存储到NSUserDefaults - Store CLLocationCoordinate2D to NSUserDefaults 在swift中CLLocation到CLLocationCoordinate2D - CLLocation to CLLocationCoordinate2D in swift 转换UnsafeMutablePointer <CLLocationCoordinate2D> 到Swift中的[CLLocationCoordinate2D] - Convert UnsafeMutablePointer<CLLocationCoordinate2D> to [CLLocationCoordinate2D] in Swift 如何从Dynamic NSMutableArray将坐标添加到CLLocationCoordinate2D? - How to add Coordinates to CLLocationCoordinate2D from Dynamic NSMutableArray? 使用 CLLocationCoordinate2D 的矩形 - Rectangle using CLLocationCoordinate2D MKPolygon - 如何确定 CLLocationCoordinate2D 是否在 CLLocationCoordinate2D 多边形中? - MKPolygon - How to determine if a CLLocationCoordinate2D is in a CLLocationCoordinate2D polygon?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM