首页 > 解决方案 > 通过点击 pin 注释转到 SwiftUI 视图

问题描述

我想通过点击 pin 注释进入新的 SwiftUI 视图 我已经在其上创建了 mapView 和 pin 注释 我想通过点击这个 pin 进入 SwiftUI 视图 如何在 Uiviewrepresentable 视图中进入 SwiftUi 视图

这是我的地图视图

func updateUIView(_ view: MKMapView, context: Context) {
let locationManager = CLLocationManager()
    let path = Datas.path
    var locationsDic: [[String: Any]] = [["latitude": 0, "longitude": 0],["latitude": 0, "longitude": 0],["latitude": 0, "longitude": 0]]

    view.showsUserLocation = true

    locationManager.requestAlwaysAuthorization()

    locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
            let locValue:CLLocationCoordinate2D = locationManager.location!.coordinate


            let coordinate = CLLocationCoordinate2D(
                latitude: locValue.latitude, longitude: locValue.longitude)

            locationsDic[0]["latitude"] = locValue.latitude.advanced(by: 0.0)
            locationsDic[0]["longitude"] = locValue.longitude.advanced(by: 0.0)

            (locationsDic as NSArray).write(toFile: path, atomically: true)


            let span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
            let region = MKCoordinateRegion(center: coordinate, span: span)
            view.setRegion(region, animated: true)


            let originAnnotation = MKPointAnnotation()
            originAnnotation.title = "origin"
            originAnnotation.coordinate = CLLocationCoordinate2D(latitude: 37.332072300, longitude: -122.011138100)


            view.addAnnotation(originAnnotation)



        })



    }
    locationsDic = NSArray(contentsOfFile: path) as! [[String: Any]]}} 

标签: mapkitswiftuimapkitannotation

解决方案


推荐阅读