简体   繁体   English

如何将String转换为CLLocationDegrees Swift 2

[英]How to convert String to CLLocationDegrees Swift 2

I am trying to convert a String that I am retrieving from Firebase and adding it as several annotations on Google Maps. 我正在尝试转换要从Firebase检索的String并将其添加为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)

    })

My JSON Tree 我的JSON树

My Code Block Which Crashes 我的代码块崩溃了

Here is the code I used for saving data to Firebase 这是我用于将数据保存到Firebase的代码

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

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 : 确保将latlon的值保存到数据库时,将它们保存为Float或Double。要检索,请使用:

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   
        }
      }
   }
})

EDIT: 编辑:

Updated code for Firebase 6 and Swift 5 更新了Firebase 6和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)
    }
})

note that self.ref points to my Firebase root ref. 请注意,self.ref指向我的Firebase根参考。

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

相关问题 将Double转换为CLLocationDegrees [SWIFT] - Convert Double to CLLocationDegrees [SWIFT] CLLocationDegrees 到 Swift 中的 String 变量 - CLLocationDegrees to String variable in Swift 如何将JSON对象转换为CLLOCATIONDEGREES? - How do I convert JSON object to CLLOCATIONDEGREES? 如何在不损失精度的情况下将NSDecimalNumber转换为CLLocationDegrees? - How to convert NSDecimalNumber to CLLocationDegrees without losing precision? 将String从CoreData转换为CLLocationDegrees / CLLocationCoordinate2D - Convert String from CoreData into CLLocationDegrees/ CLLocationCoordinate2D 无法转换“字符串”类型的值? 到预期的参数类型“CLLocationDegrees”(又名“Double”) - Cannot convert value of type 'String?' to expected argument type 'CLLocationDegrees' (aka 'Double') 如何解决:无法将“ CLLocationDegrees”类型的值(也称为“ Double”)分配为“ Float!”类型 在Swift 3中 - How to Fix: Cannot assign value of type 'CLLocationDegrees' (aka 'Double') to type 'Float!' in Swift 3 无法将CLLocationDegrees的值转换为预期的参数shopProperties - Cannot convert value of CLLocationDegrees to expected arguments shopProperties 如何快速将字符串转换为URL - How to convert string to URL in swift 如何快速转换nil字符串 - How to convert a nil string in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM