在Swift中将字符串转换为CLLocationCoordinate2D

13

使用Firebase作为我的后端,我有一系列字符串代表纬度和经度坐标,如何将它们转换为CLLocationCoordinate2D,以便我可以将它们用于注释?以下是每次更新时从Firebase获取信息的代码

var UpdateRef = Firebase(url:"https://ici.firebaseio.com/users")

UpdateRef.observeEventType(.ChildChanged, withBlock: { (snapshot) in
    let MomentaryLatitude = snapshot.value["latitude"] as? String
    let MomentaryLongitude = snapshot.value["longitude"] as? String
    let ID = snapshot.value["ID"] as? String

    println("\(MomentaryLatitude)")

    var Coordinates = CLLocationCoordinate2D(latitude: MomentaryLatitude as
        CLLocationDegrees, longitude: MomentaryLongitude as CLLocationDegrees)

}

最后一行不起作用,我应该使用什么代替?


如果您使用的是 NSString 而不是 String,那么您可以使用 doubleValue 来获取数值,然后将其用作 CLLocationCoordinate2D 的参数。 - Rob
1个回答

22

使用doubleValue属性:

let MomentaryLatitude = (snapshot.value["latitude"] as NSString).doubleValue
let MomentaryLongitude = (snapshot.value["longitude"] as NSString).doubleValue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接