【问题标题】:Map annotations only appear after map is moved地图注释仅在地图移动后出现
【发布时间】:2022-12-11 22:53:50
【问题描述】:

我有一张地图,它从 Google API 加载注释,当地图最初加载所有注释时,如控制台中的打印所示,它们是“放置”的,但是在我移动地图一次之前它们不会显示在地图上.有谁知道我是否需要在放置注释后调用方法来更新地图?

struct ContentView: View {
    
    var locationSearch = LocationSearch()
    @State private var mapView = MapView()
    @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -33.7944, longitude: 151.2649), span: MKCoordinateSpan(latitudeDelta: 0.015, longitudeDelta: 0.015))
    @EnvironmentObject var sheetManager: SheetManager

    var body: some View {
        mapView
            .popup(with: SheetManager())
            .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight)
    }
    
}
struct MapView: UIViewRepresentable {
    @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -33.7944, longitude: 151.2649), span: MKCoordinateSpan(latitudeDelta: 0.015, longitudeDelta: 0.015))
    
    func updateUIView(_ uiView: MKMapView, context: Context) {
        print("FLF: MapView updated")
        uiView.setNeedsDisplay()
    }
    
    
    var locationManager = CLLocationManager()
    let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: UIScreen.screenHeight))
    
    func setupManager() {
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
    }
    
    func makeUIView(context: Context) -> MKMapView {
        setupManager()
        mapView.region = ContentView().region
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .follow
        mapView.delegate = context.coordinator // set the delegate to the coordinator
        placeMarkersForRegion(region: region)
        return mapView
    }
    
    func placeMarkersForRegion(region: MKCoordinateRegion) {
        var locationSearch = LocationSearch()
        locationSearch.performSearch(region: region) { venues in
            print("FLF: Placing \(venues.count) marker(s)")
            for marker in venues {
                let annotation = MKPointAnnotation()
                annotation.coordinate = marker.location
                annotation.title = marker.name
                mapView.addAnnotation(annotation)
            }
        }
    }
    
    func makeCoordinator() -> MapViewCoordinator {
        MapViewCoordinator(self) // pass self to the coordinator so it can call `regionDidChangeAnimated`
    }

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        // Use the 'coordinate' property to get the current location of the map view
        let currentRegion = mapView.region
        print("FLF: Map has moved")
        self.placeMarkersForRegion(region: currentRegion)
        // Do something with the current region (e.g. update a state variable or perform a search)
    }
    
}
class MapViewCoordinator: NSObject, MKMapViewDelegate {
    var parent: MapView // add a property to hold a reference to the parent view

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

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        // Call the parent's implementation of this method
        parent.mapView(mapView, regionDidChangeAnimated: animated)
    }

  func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
      view.canShowCallout = true
      view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

          // Get the tapped annotation
          guard let annotation = view.annotation else { return }

          // Print the title of the annotation
      print(annotation.title ?? "Unknown")
  }

  func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
      print("FLF: Marker tapped")
  }
}

【问题讨论】:

    标签: ios swift swiftui mapkit mapview


    【解决方案1】:

    UIViewRepresentable 和 Coordinator 没有正确实现。

    另一个问题是 @State 不应该像您的 ContentView 有一个用于 MapView 的 @State 那样持有一个视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多