【问题标题】:How to convert String to CLLocationDegrees Swift 2如何将字符串转换为 CLLocationDegrees Swift 2
【发布时间】:2016-08-12 16:06:00
【问题描述】:

我正在尝试转换从 Firebase 检索的字符串并将其添加为 Google 地图上的多个注释。不幸的是,我的应用程序在通过当前代码时崩溃:

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

这是我用于将数据保存到 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) 字符串值。对于所有这些 ! 强制展开和强制转换操作符,这里有很多可能的崩溃来源。
  • complainLoc 是干什么用的?

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

确保将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 一样。
  • @Dravidian 另一个用户想使用这个答案,所以我添加了 Firebase 6 和 Swift 5 的更新代码。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    相关资源
    最近更新 更多