使用SwiftUI的当前位置更新MapView

问题描述 投票:-1回答:1

[尝试更新100daysOfSwiftUI的Project 14的mapview以显示我的当前位置,我无法放大移动的问题

我有此代码,我向我的MapView添加了@Binding var currentLocation : CLLocationCoordinate2Dview.setCenter(currentLocation, animated: true),所以我有一个按钮发送那个值,并且视图实际上移动到该位置的速度很慢,但随后我可以移开了]]

import SwiftUI

导入MapKit

结构MapView:UIViewRepresentable {

@Binding var centerCoordinate: CLLocationCoordinate2D
@Binding var selectedPlace: MKPointAnnotation?
@Binding var showingPlaceDetails: Bool
@Binding var currentLocation : CLLocationCoordinate2D

var annotations: [MKPointAnnotation]

func makeUIView(context: Context) -> MKMapView {
    let mapView = MKMapView()
    mapView.delegate = context.coordinator
    return mapView
}

func updateUIView(_ view: MKMapView, context: Context) {

    if annotations.count != view.annotations.count {
        view.removeAnnotations(view.annotations)
        view.addAnnotations(annotations)
    }

    view.setCenter(currentLocation, animated: true)

}

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

类协调器:NSObject,MKMapViewDelegate {

var parent: MapView
init(_ parent: MapView) {
    self.parent = parent
}

func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
     parent.centerCoordinate = mapView.centerCoordinate
 }

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
     let identifier = "PlaceMark"
     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
     if annotationView == nil {
         annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
         annotationView?.canShowCallout = true
         annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

     } else {
         annotationView?.annotation = annotation
     }

     return annotationView
 }

 func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
     guard let placemark = view.annotation as? MKPointAnnotation else {return}
     parent.selectedPlace = placemark
     parent.showingPlaceDetails = true

 }

}

}

这是我的swiftUI视图

...
    @State private var currentLocation = CLLocationCoordinate2D()

    var body: some View {
        ZStack{

            MapView(centerCoordinate: $centerCoordinate, selectedPlace: $selectedPlace, showingPlaceDetails: $showingPlaceDetails, currentLocation: $currentLocation ,  annotations: locations)
           // MapView(centerCoordinate: $centerCoordinate, selectedPlace: $selectedPlace, showingPlaceDetails: $showingPlaceDetails, annotations: locations)
                .edgesIgnoringSafeArea(.all)
            VStack{
                Spacer()
                HStack{
                    Spacer()
                    Button(action: {
                        self.getCurrentLocation()
                    }){
                        ButtonIcon(icon: "location.fill")
                    }
                }
                .padding()
            }
        }
        .onAppear(perform: getCurrentLocation)
    }

    func getCurrentLocation() {

        let lat = locationManager.lastLocation?.coordinate.latitude ?? 0
        let log = locationManager.lastLocation?.coordinate.longitude ?? 0

        self.currentLocation.latitude = lat
        self.currentLocation.longitude = log

    }
    ...

我只想在按乳酸按钮时将我的地图视图居中于当前位置

[尝试更新100daysOfSwiftUI的Project 14的mapview以显示我的当前位置,我无法放大移动的问题,我将此代码添加了@Binding var currentLocation:...

swift swiftui mkmapview
1个回答
0
投票
没有,您在哪里叫locationManager.requestWhenInUseAuthorization()。当我这样做时(当然,请确保locationManager.requestWhenInUseAuthorization()具有Info.plist的条目),它会正确更新位置。
© www.soinside.com 2019 - 2024. All rights reserved.