Convert String to CLLocationCoordinate2D in swift

余生颓废 提交于 2019-12-05 17:22:17

问题


using Firebase as my backend, I've got a series of strings that are latitude and longitude coordinates, how can I convert them to CLLocationCoordinate2D so that I can use them for annotations? Here is the code that gets the info from Firebase every time its updated

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)

}

The last line doesn't work, what should I use instead?


回答1:


Use the doubleValue property:

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


来源:https://stackoverflow.com/questions/28417512/convert-string-to-cllocationcoordinate2d-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!