【问题标题】:SwiftUI: replacing custom map button with a MapPin as a button?SwiftUI:用 MapPin 作为按钮替换自定义地图按钮?
【发布时间】:2021-06-04 22:35:37
【问题描述】:

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

【问题讨论】:

    标签: swiftui swiftui-navigationlink


    【解决方案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”

    并根据需要调整颜色。

    【讨论】:

    • 这很好用。再次感谢您的所有帮助:)
    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2019-12-12
    相关资源
    最近更新 更多