如何将String转换为CLLocationDegrees Swift 2 [英] How to convert String to CLLocationDegrees Swift 2

查看:150
本文介绍了如何将String转换为CLLocationDegrees Swift 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试转换要从Firebase检索的字符串,并将其添加为Google Maps上的多个注释.不幸的是,只要通过当前代码,我的应用就会崩溃:

I am trying to convert a String that I am retrieving from Firebase and adding it as several annotations on Google Maps. Unfortuanately, my app is crashing whenever it goes through the current code:

ref = FIRDatabase.database().reference()
    ref.child("Locations").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        let lat = (snapshot.value!["Latitude"] as! NSString).doubleValue
        let lon = (snapshot.value!["Longitude"] as! NSString).doubleValue



        let complainLoc = CLLocationCoordinate2DMake(lat, lon)

        let Coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon)

    })

我的JSON树

崩溃的我的代码块

这是我用于将数据保存到Firebase的代码

Here is the code I used for saving data to Firebase

FIRDatabase.database().reference().child("Location").child(FIRAuth.auth()!.currentUser!.uid).setValue(["Latitude": locationManager.location!.coordinate.latitude, "Longitude": locationManager.location!.coordinate.longitude])

推荐答案

确保将latlon的值保存到数据库时,将它们保存为Float或Double. 检索使用:

Make sure when you are saving the values of lat and lon to the database you are saving them as Float or Double.. For retrieving use :

ref = FIRDatabase.database().reference()
    ref.child("Locations").observeEventType(.Value, withBlock: { (snapshot) in

   if snapshot.exists(){       

    if let locationDictionary = snapshot.value as [String : AnyObject]{

      for each in locationDictionary{          

     //each will bring you every location dictionary in your database for your every user
             let lat = each.value!["Latitude"] as! CLLocationDegrees
             let lon = each.value!["Longitude"] as! CLLocationDegrees
             let userId = each.key as! String

             let complainLoc = CLLocationCoordinate2DMake(lat, lon)

             let Coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon)   
       //Every time this for loop complete's itself it will generate a new set of Coordinates for each user   
        }
      }
   }
})

Firebase 6和Swift 5的更新代码

Updated code for Firebase 6 and Swift 5

let ref = self.ref.child("Locations")
ref.observeSingleEvent(of: .value, with: { snapshot in
    let allLocations = snapshot.children.allObjects as! [DataSnapshot]
    for location in allLocations {
        let lat = location.childSnapshot(forPath: "Latitude").value as! CLLocationDegrees
        let lon = location.childSnapshot(forPath: "Longitude").value as! CLLocationDegrees
        let userId = location.key
        let locCoord = CLLocationCoordinate2DMake(lat, lon)
        let coordinates  = CLLocationCoordinate2D(latitude: lat, longitude: lon)
    }
})

请注意,self.ref指向我的Firebase根引用.

note that self.ref points to my Firebase root ref.

这篇关于如何将String转换为CLLocationDegrees Swift 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆