我正在尝试添加一个MapAnnotation
项目到我的Map()
查看自MapKit
.
如果您在文本字段中手动添加坐标,则一切正常,但我找不到任何通过点击地图来添加坐标的方法。
我在互联网上阅读了很多内容,但没有找到任何内容onTap()
地图事件处理。
这是我到目前为止的地图视图的样子:
struct MyMap : View {
@State private var myRegion:MKCoordinateRegion = {
var newRegion = MKCoordinateRegion()
newRegion.center.latitude = 53.7576508
newRegion.center.longitude = -11.1811597
newRegion.span.latitudeDelta = 0.1
newRegion.span.longitudeDelta = 0.1
return newRegion
}()
@State private var annotationItems : [Annotation] = [
Annotation(name: "Pin1", description: "myDescription", coordinate: CLLocationCoordinate2D(latitude: 123.7576508, longitude: -7.2373215))
]
var body: some View {
ZStack {
Map(coordinateRegion: self.$myRegion, interactionModes: .all, annotationItems: annotationItems){ item in
MapAnnotation(coordinate: item.coordinate){
PinView(pin: item)
}
}
.edgesIgnoringSafeArea(.all)
}
}
}
有什么方法可以获取用户在地图上点击的位置的坐标吗?
iOS 17 现在有一个用于 SwiftUI 地图的 MapReader。你可以像这样包装 Map:
struct ContentView: View {
var body: some View {
MapReader { reader in
Map()
.onTapGesture(perform: { screenCoord in
let pinLocation = reader.convert(screenCoord, from: .local)
print(pinLocation)
})
}
}
}
您可以提取触摸的纬度/经度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)