使用 Pin 将坐标传输到 MapView

Transferring Coordinates to a MapView with Pin

我正在尝试在地图上显示地图图钉。输入交易后,详细信息将与位置坐标一起保存。在交易条目列表中,用户可以单击条目以获取更多详细信息,包括显示交易位置的小地图。

根据 Asperi 在 的建议,我似乎需要声明一个可识别的结构才能使用地图图钉。

在 DetailView 中,纬度和经度在传输到 MapView 之前被复制到坐标参数。

struct DetailView: View {
       
    var coordinate: CLLocationCoordinate2D {
          CLLocationCoordinate2D(
          latitude: item.entryLat,
          longitude: item.entryLong)
     }
        
        var body: some View {
            
            VStack {
                MapView(coordinate: coordinate)
                    .ignoresSafeArea(edges: .all)
                    .frame(height: 400)
                    .padding(.vertical, 10)
            }
       }
  }

MapView 是我遇到问题的地方。我不确定如何传递我的区域坐标和标记 (xxxxx)。将坐标复制到 @State 区域和标记会产生错误“传递给不带参数的调用的参数”。

struct Marker: Identifiable {
 let id = UUID()
 var location: MapMarker
}


struct MapView: View {
    
    var coordinate: CLLocationCoordinate2D
    
    @State private var region = MKCoordinateRegion(center:    CLLocationCoordinate2D(xxxxxxx), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
    
    let markers = [Marker(location: MapMarker(coordinate: CLLocationCoordinate2D(xxxxxxx), tint: .red))]
    
    var body: some View {
        
            Map(coordinateRegion: $region, showsUserLocation: true,
              annotationItems: markers) { marker in
                marker.location
            }.edgesIgnoringSafeArea(.all)
    }
}

听起来很适合您的应用程序,将区域声明为常量即可。代码如下所示:

struct Marker: Identifiable {
    let id = UUID()
    var location: MapMarker
}

struct MapView: View {
    var coordinate: CLLocationCoordinate2D
    
    var body: some View {
        
        Map(coordinateRegion: .constant(MKCoordinateRegion(center: coordinate,
                                                           span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))),
            showsUserLocation: true,
            annotationItems: [Marker(location: MapMarker(coordinate: coordinate))]) { marker in
                marker.location
        }.edgesIgnoringSafeArea(.all)
    }
}

如果您仍然想将它用作@State 变量,您可以使用自定义 init 来设置值:

struct MapView: View {
    var coordinate: CLLocationCoordinate2D
    
    @State private var region : MKCoordinateRegion
    
    init(coordinate : CLLocationCoordinate2D) {
        self.coordinate = coordinate
        _region = State(initialValue: MKCoordinateRegion(center: coordinate,
                                                             span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)))
    }
    
    var body: some View {
        Map(coordinateRegion: $region,
            showsUserLocation: true,
            annotationItems: [Marker(location: MapMarker(coordinate: coordinate))]) { marker in
                marker.location
        }
        .edgesIgnoringSafeArea(.all)
    }
}

最后,我定义了内联标记数组,但您可以将其拆分为一个计算 属性:


    var markers : [Marker] {
        [Marker(location: MapMarker(coordinate: coordinate))]
    }
    
    var body: some View {
        Map(coordinateRegion: $region,
            showsUserLocation: true,
            annotationItems: markers) { marker in
                marker.location
        }
        .edgesIgnoringSafeArea(.all)
    }