使用 SwiftUI 在 MapKit 中放置标记

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

我正在尝试在 Map() 语句中调整地图的范围。这是针对高尔夫应用程序的,根据球洞位置,我想限制该特定球洞的地图范围。当前的代码覆盖了整个高尔夫球场,但是当我将坐标区域添加到 Map() 时,我收到一条错误消息...我对自己做错了什么感到茫然...

这里是有效的代码..但是,一旦我将“Map()”更改为“Map(coordinateRegion: $mapRegion)”,我就会收到一条错误消息“闭包参数列表的上下文类型需要 1 个参数,这不能是隐式忽略”

struct ContentView: View {
    
    @State var current_hole: Int = 0
    @StateObject var locationManager = LocationManager()
    @State var mapRegion = MKCoordinateRegion( center:CLLocationCoordinate2D(latitude: 29.65,         longitude: -95.9333), span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03))
    

    
    var userLatitude: String {
        return "\(locationManager.lastLocation?.coordinate.latitude ?? 0)"
    }
    var userLongitude: String {
        return "\(locationManager.lastLocation?.coordinate.longitude ?? 0)"
    }
    
      
        var body: some View {

        Map(){ // error if changed to Map(coordinaterRegion: $mapRegion)
            
            Marker("", coordinate: wf_pin_coordinates[0])
            Marker("",  coordinate: wf_pin_coordinates[1])
            Marker("",  coordinate: wf_pin_coordinates[2])
            //... and so on
        }.mapStyle(.hybrid)
                
        HStack {
            Button(("Hole "+String(current_hole+1))) {
                current_hole = current_hole + 1
                if ( current_hole > 17 ) {
                    current_hole = 0
                }
            }
            Text(hole_description[current_hole]).fontWeight(.bold)
        }
            
        }
            
    }



#Preview {
    ContentView()
}
swiftui mapkit google-maps-markers
1个回答
0
投票

最新版本的

Map
结构有一些变化。

更换

@State var mapRegion = MKCoordinateRegion( center:CLLocationCoordinate2D(latitude: 29.65,         longitude: -95.9333), span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03))

@State private var cameraPosition = MapCameraPosition.region(
    MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 29.65, longitude: -95.9333),
                       span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03)))

Map() {

Map(position: $cameraPosition) {
© www.soinside.com 2019 - 2024. All rights reserved.