How to convert String to CLLocationDegrees Swift 2

∥☆過路亽.° 提交于 2019-12-24 01:41:21

问题


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)

    })

My JSON Tree

My Code Block Which Crashes

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

回答1:


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

EDIT:

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

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




回答2:


SWIFT 5

let dbLat = Double(latStr)  // Convert String to double
let dbLong = Double(longStr)

Use latitude and longitude

let center = CLLocationCoordinate2D(latitude: dbLat! , longitude: dbLong! )

let pointAnnotation = MKPointAnnotation()
 pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: dbLat!, longitude:dbLong!)


来源:https://stackoverflow.com/questions/38922258/how-to-convert-string-to-cllocationdegrees-swift-2

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