首页 > 解决方案 > 制作当前位置。Mapkit 的 swiftui 按钮

问题描述

我使用 Mapkit 和 uirepresentable 视图制作了一个地图视图,现在我想制作一个按钮,当我按下它时,它会给出我当前的位置。我用 swiftui 制作了一个按钮,但我不知道如何在 mapview 中使用该按钮。你可以在那里看到 mapview.swift 代码。

func updateUIView(_ view: MKMapView, context: Context) {
    let locationManager = CLLocationManager()

    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
                print("CURRENT LOCATION = \(locValue.latitude) \(locValue.longitude)")

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

            })




    }
}

标签: iosswiftui

解决方案


推荐阅读