如何获取可见地图显示上角点的坐标? (在新的 SwiftUI Map 对象中)

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

获取 SwiftUI 中可见地图显示上的角点坐标(即 CLLocationCooperative2D)的代码是什么?

注意,这是使用新的 Map/SwiftUI 方法。

假设我在 SwiftUI 中

var body: some View {
    ZStack {
        MapReader { mapProxy in
            Map(position: $mapCamPos) {
               .
               .
               .
            }
            .onReceive(locationMgr.$location) { location in

                    // HOW TO DERIVE CO-ORDINATES AT THIS POINT???

                    mapCamPos =  .camera(MapCamera(
                        centerCoordinate: location.coordinate,
                        distance: distance,
                        heading: locationMgr.headingDegrees
                    ))
             }
     }
 }
                
swift swiftui mapkit
2个回答
0
投票

你必须使用

mapProxy
中的
MapReader
东西。它就像地图的好帮手。在
onReceive
部分内,查看
mapProxy.visibleRegion
- 这是地图左下角和右上角坐标悬停的位置。从那里获取这些坐标,然后你就可以用它们做任何你需要的事情了,你知道吗?因此,当位置更新时,只需从
mapProxy.visibleRegion
中抓取这些角点,然后让您的地图根据您需要做的事情来完成它的工作,就像魔术一样!


0
投票

您应该使用

onMapCameraChange
和附加状态来跟踪地图相机的变化:

Map {
    ...
}
.onMapCameraChange(frequency: .continuous /* or .onEnd depending on your needs */) { context in
    // currentMapRect is a @State that you should add to your view
    currentMapRect = context.rect
}

然后,在

onReceive
或其他任何地方,您可以使用
minX
minY
maxX
maxY
currentMapRect
属性来查找四个角的坐标。请注意,您需要将这些 x-y 值转换为地理坐标,例如

let bottomRightPoint = MKMapPoint(x: currentMapRect.maxX, y: currentMapRect.maxY)
let bottomRightCoordinate = bottomRightPoint.coordinate
© www.soinside.com 2019 - 2024. All rights reserved.