问题描述
我正在尝试在一个应用中实现一个功能,在该应用中,管理应用共享其位置,即将地理位置保存到Firebase,最终用户应用从存储的数据库中读取该位置并能够导航到该位置。
我遇到的问题是管理应用程序保存了位置并没有回读任何问题,但是最终用户不断抛出错误,指出在展开Optional值时意外发现nil。我不知道我在想什么
++++++++++++++++++++++++++++++ (我导入了所有必需的库)
@Observedobject var obs =观察者()
var body: some View {
NavigationView {
ZStack(alignment: Alignment.topLeading) {
mapView(name: self.name,geopoints:self.obs.data["data"] as! [String: GeoPoint] ).edgesIgnoringSafeArea(.top)
导入SwiftUI 导入CoreLocation 导入MapKit 导入Firebase
struct mapView:UIViewRepresentable {
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")
}
}
}
}
类观察者:ObservableObject {
@Published var data = String:任何
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
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)