Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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])
like image 752
Ishan Jain Avatar asked Dec 25 '22 02:12

Ishan Jain


1 Answers

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!)
like image 63
kashish makkar Avatar answered Dec 28 '22 09:12

kashish makkar