如何在 SwiftUI 2 上的地图注释上显示工作表?

问题描述

我做了一个有几个注解的地图,显示注解和地图没有问题。 但是当我们点击时,我想在表格中显示这些注释中的详细信息(名称、城市等),我有错误

我遇到的错误: 当我们单击其中一个注释时,它会向我显示错误的详细视图,除非我在单击其中一个注释之前先在地图上移动。

地图视图

import Foundation
import SwiftUI
import MapKit

struct MapView: View {
    @StateObject private var manager = LocationManager(CLLocation(latitude: 48.862725,longitude: 2.287592))
    @State private var trackingMode = MapUserTrackingMode.follow
    var location = Locationsviewmodel()
    @State var showSheet = false
    @State var currentLocation: Locations
    @State var tracker = false
    var body: some View {
        
        ZStack{
            Map(coordinateRegion: $manager.region,interactionModes: .all,showsUserLocation: true,userTrackingMode: $trackingMode,annotationItems: location.getLocations(),annotationContent:{location in
                
                MapAnnotation(coordinate: location.coordinate(),anchorPoint: CGPoint(x: 0.5,y: 0.5)) {
                  
                    Button(action: {
                        showSheet.toggle()
                        
                    },label: {
                        Image("location")
                            .resizable()
                            .frame(width: 30,height: 30)
                            .foregroundColor(.red)
                    }).sheet(isPresented: $showSheet) {
                            Text(location.address)
                     
                        }
                }
                
            } )
            
            //            Map(coordinateRegion: $manager.region,userTrackingMode: $trackingMode)
        }
        
        .alert(isPresented: $manager.permissionDenied,content: {
            Alert(title: Text("Permission denied"),message: Text("put it on "),dismissButton: .default(Text("go to setting"),action: {
                
                UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
                
            }))
        })
        
    }
}

struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView(currentLocation: Locations(address: "Rue Jean Jaurès",country: "Villeneuve d'ascq",zipCode: "59491",latitude: 50.659419,longitude: 3.135344))
    }
}

位置管理器

    import Foundation
import SwiftUI
import MapKit

class LocationManager: NSObject,ObservableObject,CLLocationManagerDelegate {
    @Published var region = MKCoordinateRegion()
    @Published var permissionDenied = false
    @Published var tracker = false
   
    var lastUpdateLocation:  CLLocation?
    var span: MKCoordinateSpan
    let manager = CLLocationManager()
    init(_ location: CLLocation) {
        span = MKCoordinateSpan(latitudeDelta: 0.05,longitudeDelta: 0.05)
        region = MKCoordinateRegion(center: location.coordinate,span: span )
        super.init()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
      
    }
    
    
    
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        switch manager.authorizationStatus {
        case .authorizedAlways: print("authorized always")
        case .denied:
            permissionDenied.toggle()
        case .notDetermined: manager.requestWhenInUseAuthorization()
            print("not determined")
        case .authorizedWhenInUse:
            manager.requestLocation()
            print("authorizedWhenInUse")
        case .restricted:
            print("restricted")
            print("authorizedAlways")
           
        @unkNown default:
            print("unkNown")
        }
    }
    
    func locationManager(_ manager: CLLocationManager,didFailWithError error: Error){
        
        print(error.localizedDescription)
    
    }
    
    func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
        //        locations.last.map {
        //            let center = CLLocationCoordinate2D(latitude: $0.coordinate.latitude,longitude: $0.coordinate.longitude)
        //            let span = MKCoordinateSpan(latitudeDelta: 0.05,longitudeDelta: 0.05)
        //            region = MKCoordinateRegion(center: center,span: span)
        //
        //        }
      
        if let lastUpdated = locations.last {
            
            if((lastUpdateLocation == nil ) || (lastUpdateLocation != nil && tracker )){
                
                center(newLocation: lastUpdated)
            }
            
        }
        
    }
    
    func center(newLocation: CLLocation){
        lastUpdateLocation = newLocation
        region = MKCoordinateRegion(center: newLocation.coordinate,span: span )
    }
}

数据和模型

    import Foundation

struct Locationsviewmodel {
    
    func getLocations() -> [Locations] {
        return [
            Locations(address: "Rue Jean Jaurès",longitude: 3.135344),Locations(address: "Rue du Colibri",country: "villeneuve d'ascq",zipCode: "59650",latitude: 50.638455,longitude: 3.1508),Locations(address: "52 Rue du Maréchal Foch",country: "Lezennes",zipCode: "59260",latitude: 50.618288,longitude: 3.112348),Locations(address: "132 Rue de Valenciennes",country: "Lille",zipCode: "59033",latitude: 50.623385,longitude: 3.076643),]
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...