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

时间:2021-06-04 22:35:37

标签: swiftui swiftui-navigationlink

我在地图上使用了自定义按钮,但现在我意识到我想使用普通图钉,例如 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() 作为最后的努力,但没有奏效。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

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”

并根据需要调整颜色。