如何将字符串转换为CLLocationDegrees Swift 2

4

我正在尝试将从Firebase检索到的字符串转换为Google Maps上的多个注释。不幸的是,每当它经过当前代码时,我的应用程序就会崩溃:

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的代码。

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

你确定它是在创建CLLocationCoordinate2D时崩溃,而不是在进行强制转换/解包的地方?检查确保value不是nil。还要检查value["Latitude"]value["Longitude"]都不是(a) nil,且(b)是字符串值。由于所有这些!强制解包和强制转换运算符,这里有很多可能导致崩溃的源头。 - Rob
complainLoc 是用来做什么的? - vadian
2个回答

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

使用纬度和经度

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

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

5

在保存latlon的数值到数据库中时,请确保将它们保存为浮点或双精度类型。在检索时使用:

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 的代码

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 根 ref。

该应用程序在我创建let变量lat和lon时仍然崩溃。我已正确输入纬度和经度,就像保存数据到Firebase时所做的那样。 - Ishan Jain
1
@Dravidian 另一个用户想要使用这个答案,所以我添加了适用于 Firebase 6 和 Swift 5 的更新代码。 - Jay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接