如果我们在整个地图上有多个注释,我们如何确定我们的地图区域

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

此代码工作完全正常,尽管当我有来自不同不同区域的大量注释时,我无法设置地图区域以便我可以在屏幕上看到所有注释。这就是为什么有些注释不可见的原因。

我们如何计算我的地图区域,以便每个注释都可以显示在屏幕上。

struct MapViewWithAnnotatios: UIViewRepresentable {
var viewModel: MapViewWithAnnotationsViewModel
// Location Manager object to request location updates from the core API
// Annotations array
var annotations: [MKPointAnnotation]?
var mapRegion:  MKCoordinateRegion
// @State var mapView: MKMapView?
var mapRegion1: MKCoordinateRegion?
// Setup function for the representable
func setup() {
    // Set other properties if needed
}

// Function to configure the representable view
func makeUIView(context: Context) -> MKMapView {
    // Call the setup function
    setup()
    // Initialize the representable view
    let mapView = MKMapView(frame: UIScreen.main.bounds)
    mapView.delegate = context.coordinator
    

    DispatchQueue.main.async {
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .follow
    }
     
    // Provide annotations if they exists
    if let annotations = annotations {
        mapView.showAnnotations(annotations, animated: true)
    }
    mapView.setRegion(mapRegion, animated: true)
    // Return the representable view
    if viewModel.isCircleOverlayNeeded {
        let miles = 3.0
        let meters = miles * 1609.34
        let circle = MKCircle(center: mapView.region.center, radius: meters)
        mapView.setVisibleMapRect(circle.boundingMapRect, edgePadding: .init(top: 20, left: 50, bottom: 20, right: 50) ,animated: true)
        mapView.addOverlay(circle)
    }
    return mapView
}

func updateUIView(_ uiView: MKMapView, context: Context) {
    print("updateUIView")
}
swiftui mapkit mkmapview
1个回答
0
投票

您的代码中已经有了解决方案:

if let annotations = annotations {
    mapView.showAnnotations(annotations, animated: true)
}

这会更改可见区域以在屏幕上显示每个注释。

下一行就毁掉了你的解决方案:

mapView.setRegion(mapRegion, animated: true)

一个可能的解决方案是

if let annotations = annotations {
    mapView.showAnnotations(annotations, animated: true)
} else {
    mapView.setRegion(mapRegion, animated: true)
} 
© www.soinside.com 2019 - 2024. All rights reserved.