Ios Swift 5从firebase到MapView的加载位置出错
我试图在应用程序中实现一个功能,其中管理员应用程序共享其位置,即将地理点保存到firebase,最终用户应用程序从存储的数据库中读取位置,并能够导航到所述位置 我遇到的问题是,管理员应用程序保存位置并将其读回没有问题,但是最终用户在展开可选值时不断抛出一个错误,表示意外发现nil。我不知道我错过了什么 此片段在内容视图中加载地图 +++++++++++++++++++++++++++ (I导入所有必需的LIB) @ObservedObject变量obs=观察者() 然后我有另一个地图视图文件 导入快捷键 导入核心定位 导入地图套件 进口火基 结构映射视图:UIViewRepresentable{Ios Swift 5从firebase到MapView的加载位置出错,ios,swift,mapkit,Ios,Swift,Mapkit,我试图在应用程序中实现一个功能,其中管理员应用程序共享其位置,即将地理点保存到firebase,最终用户应用程序从存储的数据库中读取位置,并能够导航到所述位置 我遇到的问题是,管理员应用程序保存位置并将其读回没有问题,但是最终用户在展开可选值时不断抛出一个错误,表示意外发现nil。我不知道我错过了什么 此片段在内容视图中加载地图 +++++++++++++++++++++++++++ (I导入所有必需的LIB) @ObservedObject变量obs=观察者() 然后我有另一个地图视图文件 导
var name = ""
var geopoints : [String : GeoPoint]
//var ShareLoc:Bool
func makeCoordinator() -> mapView.Coordinator {
return mapView.Coordinator(parent1: self)
}
let map = MKMapView()
let manager = CLLocationManager()
func makeUIView(context: UIViewRepresentableContext<mapView>) -> MKMapView {
self.manager.requestAlwaysAuthorization()
self.manager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
manager.delegate = context.coordinator
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
map.mapType = .standard
map.isZoomEnabled = true
map.isScrollEnabled = true
if let coor = map.userLocation.location?.coordinate {
map.setCenter(coor, animated: true)
}
return map
}
func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<mapView>) {
for i in geopoints{
if i.key != name{
let point = MKPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: i.value.latitude, longitude: i.value.longitude)
point.title = i.key
uiView.removeAnnotations(uiView.annotations)
uiView.addAnnotation(point)
}
}
}
class Coordinator : NSObject,CLLocationManagerDelegate{
var parent : mapView
init(parent1 : mapView) {
parent = parent1
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .denied{
print("denied")
}
if status == .authorizedWhenInUse{
print("authorized")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locVal:CLLocationCoordinate2D = manager.location!.coordinate
parent.map.mapType = MKMapType.standard
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: locVal, span: span)
parent.map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = locVal
annotation.title = "somewhere"
annotation.subtitle = "Current location"
parent.map.addAnnotation(annotation)
let last = locations.last
let db = Firestore.firestore()
db.collection("locations").document("sharing").setData(["updates" : [self.parent.name : GeoPoint(latitude: (last?.coordinate.latitude)!, longitude: (last?.coordinate.longitude)!)]],merge: true) { (err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
print("success")
}
}
}
}
你能粘贴堆栈跟踪吗?谢谢你的提问,我在帖子中添加了一个截图。希望有帮助
func makeCoordinator() -> mapView.Coordinator {
return mapView.Coordinator(parent1: self)
}
let map = MKMapView()
let manager = CLLocationManager()
func makeUIView(context: UIViewRepresentableContext<mapView>) -> MKMapView {
self.manager.requestAlwaysAuthorization()
self.manager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
manager.delegate = context.coordinator
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
map.mapType = .standard
map.isZoomEnabled = true
map.isScrollEnabled = true
if let coor = map.userLocation.location?.coordinate {
map.setCenter(coor, animated: true)
}
return map
}
func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<mapView>) {
for i in geopoints{
if i.key != name{
let point = MKPointAnnotation()
point.coordinate = CLLocationCoordinate2D(latitude: i.value.latitude, longitude: i.value.longitude)
point.title = i.key
uiView.removeAnnotations(uiView.annotations)
uiView.addAnnotation(point)
}
}
}
class Coordinator : NSObject,CLLocationManagerDelegate{
var parent : mapView
init(parent1 : mapView) {
parent = parent1
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .denied{
print("denied")
}
if status == .authorizedWhenInUse{
print("authorized")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locVal:CLLocationCoordinate2D = manager.location!.coordinate
parent.map.mapType = MKMapType.standard
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let region = MKCoordinateRegion(center: locVal, span: span)
parent.map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = locVal
annotation.title = "somewhere"
annotation.subtitle = "Current location"
parent.map.addAnnotation(annotation)
let last = locations.last
let db = Firestore.firestore()
db.collection("locations").document("sharing").setData(["updates" : [self.parent.name : GeoPoint(latitude: (last?.coordinate.latitude)!, longitude: (last?.coordinate.longitude)!)]],merge: true) { (err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
print("success")
}
}
}
init() {
let db = Firestore.firestore()
db.collection("locations").document("sharing").addSnapshotListener { (snap, err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
let updates = snap?.get("updates") as? [String : GeoPoint]
self.data["data"] = updates
}
}