4

我决定从将 MKMapView 包装成 UIViewRepresentable 切换到 SwiftUI 中的新Map()功能。

我能够正确显示一个MKMapRectMap()但我无法在MKPointAnnotation那里显示两个。我在这些注释之间的路线也没有显示

它要求我提供一个RandomAccessCollection和一个(Identifiable) -> MapAnnotationProtocol>,但我不知道该放什么。

知道我应该放(Identifiable) -> MapAnnotationProtocol什么吗?

import SwiftUI
import MapKit

extension MKPointAnnotation: Identifiable { }

struct ItemView: View {
    @ObservedObject var itemViewModel: ItemViewModel
    @State var coll = [MKPointAnnotation]()
    
    func onAppear() {
        let requestAnnotation = MKPointAnnotation()
        requestAnnotation.coordinate = CLLocationCoordinate2D(latitude: 46.2004781, longitude: 6.1346497)
        requestAnnotation.title = "Package"
        
        let destinationAnnotation = MKPointAnnotation()
        destinationAnnotation.coordinate = CLLocationCoordinate2D(latitude: 47.1420446, longitude: 9.5204032)
        destinationAnnotation.title = "Destination"
        
        coll.append(requestAnnotation)
        coll.append(destinationAnnotation)
    }
    
    var body: some View {
        VStack {
            if let mapRect = itemViewModel.route?.polyline.boundingMapRect {
                Map(mapRect: .constant(mapRect), annotationItems: coll) { point in
                  MapAnnotation(coordinate: point.coordinate) {
                      Text(point.title ?? "")
                  }
                }
            }
        }
        .onAppear(perform: self.onAppear)
    }
    
}
4

4 回答 4

3

你不需要MKPointAnnotation再做。目前,在 Xcode 12.0 Beta 中,SwiftUI 提供了三个结构来满足MapAnnotationProtocol

  • MapAnnotation
  • MapMarker
  • MapPin

RandomAccessCollection应该是采用Identifiable. Map这些在初始化程序的最后一个参数中用作块的参数。

Map(coordinateRegion: $container.region, 
    annotationItems: container.annotationLocations) { 
        (location) -> MapPin in
            MapPin(coordinate: location.coordinate)
    })
于 2020-06-30T23:00:33.170 回答
3

在我看来,这MapAnnotation需要您实现将在地图上呈现的整个视图,因此,如果您需要红色图钉和一些文本,则必须自己创建该图钉(例如,作为带有 SF Symbol 的 Image 实例)。当然,它看起来不如图钉那么好,MapMarker但至少它是一个开始。

Map(coordinateRegion: .constant(mapRect), annotationItems: coll) { annotationItem in
  MapAnnotation(coordinate: annotationItem.coordinate) {
    VStack {
      Group {
        Image(systemName: "mappin.circle.fill")
          .resizable()
          .frame(width: 30.0, height: 30.0)
        Circle()
          .frame(width: 8.0, height: 8.0)
      }
      .foregroundColor(.red)
      Text(annotationItem.title)
    }
  }
}
于 2020-08-19T13:05:16.283 回答
1

它应该如下所示(由于提供的快照中缺少依赖关系而未测试)

Map(mapRect: .constant(mapRect), annotationItems: coll) { point in
   MapAnnotation(coordinate: point.coordinate) {
    Text(point.title ?? "")
   }
}
于 2020-06-24T04:43:58.180 回答
0

对于任何搜索 SwiftUI MKPolyline 路线的人

    struct MapView: View {
        
        @ObservedObject var viewModel: MapViewModel
        
        var body: some View {
            VStack(spacing: 0) {
                Text("Status")
                    .padding()
                MapViewRepresentable(exampleRoute: viewModel.exampleRoute)
                Button("Button", action: { })
                    .padding()
            }
        }
        
    }
    
    
    struct MapView_Previews: PreviewProvider {
        static var previews: some View {
            MapView(viewModel: MapViewModel())
        }
    }
    
    
    struct MapViewRepresentable: UIViewRepresentable {
        
        private let initialRegion = CoordinateRegion.initial
        
        let exampleRoute: MKPolyline
        
        func makeCoordinator() -> Coordinator {
            return Coordinator(self)
        }
        
        func makeUIView(context: UIViewRepresentableContext<MapViewRepresentable>) -> MKMapView {
            let mapView = MKMapView()
            mapView.delegate = context.coordinator
            mapView.setRegion(initialRegion, animated: false)
            mapView.showsUserLocation = true
            mapView.addOverlay(exampleRoute)
            return mapView
        }
    
        func updateUIView(_ view: MKMapView,
                          context: UIViewRepresentableContext<MapViewRepresentable>) {
    
        }
        
        class Coordinator: NSObject, MKMapViewDelegate {
            let parent: MapViewRepresentable
            init(_ mapView: MapViewRepresentable) {
                parent = mapView
            }
            func mapView(_ mapView: MKMapView,
                         rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
                let polylineRenderer: MKPolylineRenderer = MKPolylineRenderer(overlay: overlay)
                polylineRenderer.lineWidth = 3
                polylineRenderer.strokeColor = UIColor.red
                return polylineRenderer
            }
        }
        
    }


class MapViewModel: ObservableObject {
    
    @Published private(set) var exampleRoute = createPolylines()
    
    static func createPolylines() -> MKPolyline {
        var coordinates1 = createExampleRoute()
        return MKPolyline(coordinates: &coordinates1, count: coordinates1.count)
    }
    
    static func createExampleRoute() -> [CLLocationCoordinate2D] {
        return [CLLocationCoordinate2D(latitude: 60.608905, longitude: 39.814428),
                CLLocationCoordinate2D(latitude: 60.609073, longitude: 39.812000),
                CLLocationCoordinate2D(latitude: 60.610429, longitude: 39.812071)]
    }
    
}


struct CoordinateRegion {
    static let initial = MKCoordinateRegion(center: CLLocationManager().location?.coordinate
                                                    ?? CLLocationCoordinate2D(latitude: 60.608905,
                                                                              longitude: 39.814428),
                                            span: MKCoordinateSpan(latitudeDelta: 0.05,
                                                                   longitudeDelta: 0.05))
    func getCurrentLocation() -> CLLocationCoordinate2D {
        return CLLocationManager().location!.coordinate
    }
    
}
于 2021-07-02T17:21:45.743 回答