我想從我的應用中添加我的位置值,並將它們轉換爲註釋。這是我一分鐘沒有工作的代碼!尋找一個援助之手,感覺就像我非常接近這個權利,但我已經打了一個大腦!Firebase地圖到地圖
import UIKit
import Firebase
import MapKit
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
let locationsRef = FIRDatabase.database().reference(withPath: "locations")
override func viewDidLoad() {
super.viewDidLoad()
locationsRef.observe(.value, with: { snapshot in
for item in snapshot.children {
guard let locationData = item as? FIRDataSnapshot else { continue }
let locationValue = locationData.value as! [String: Any]
let location = CLLocationCoordinate2D(latitude: locationValue["lat"] as! Double, longitude: locationValue["lng"] as! Double)
let name = locationValue["name"] as! String
self.addAnnotation(at: location, name: name)
}
})
}
func addAnnotation(at location: CLLocationCoordinate2D, name: String) {
// add that annotation to the map
let annotation = MKPointAnnotation()
annotation.title = location["name"] as? String
annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["lonitude"] as! Double)
mapView.addAnnotation(annotation)
}
}
我的代碼是現在的工作,但我得到這個問題:
Could not cast value of type "NSTaggedPointerString' (0x10a708d10) to 'NSNumber' (0x109d10488).
與這行代碼:
let location = CLLocationCoordinate2D(latitude: locationValue["lat"] as! Double, longitude: locationValue["lng"] as! Double)
我讀過一些有關將字符串轉換成翻倍但是這行中沒有字符串?任何人都可以幫忙嗎?
更新的代碼字幕
更新錯誤
請解釋 「不工作」,包括樣本你的JSON數據。 –
你在這裏工作太辛苦 - 在你的方法'addAnnotation'中,你再也沒有字典了,你已經有了一個位置和一個名字 - 只需要使用這些變量 – Russell