SwiftUI:用 MapPin 作为按钮替换自定义地图按钮?

SwiftUI: replacing custom map button with a MapPin as a button?

我在地图上使用自定义按钮,但我现在意识到我想使用普通图钉,例如 MapPin。如何更改我的代码以针对 MapPin 的使用进行调整,以便在我单击它时发生相同的操作。

   Map(coordinateRegion: $region, annotationItems: getAnnotated()) { item in
            MapAnnotation(coordinate: item.coordinate) {
                Button(action: {
                    selected.item = item  // <--- here
                    showSheet.toggle()
                }){
                    Image("icon")
                        .resizable()
                        .frame(width: 70, height: 70)
                        .foregroundColor(.white)
                        .cornerRadius(9.0)
                }
                .background(Circle())
                .foregroundColor(Color.green)
            }
        }

我尝试将 Image() 换成 MapPin() 作为最后的努力,但没有奏效。

你可以试试这个:

Map(coordinateRegion: $region, annotationItems: getAnnotated()) { item in
    MapAnnotation(coordinate: item.coordinate) {
        Button(action: {
            selected.item = item
            showSheet.toggle()
        }){
            Image(systemName: "mappin.and.ellipse")  // <---
                .scaleEffect(2.0)
                .foregroundColor(.red)
                .padding()
        }
        .background(Circle())
        .foregroundColor(Color.clear)  // <---
    }
}

或“mappin.circle”、“mappin”

并根据需要调整颜色。