swift 从mapView?regionDidChanged快速获取视口坐标

tzdcorbm  于 2023-01-19  发布在  Swift
关注(0)|答案(2)|浏览(126)

当用户移动Map时调用以下函数。
可以得到视口吗?视口是北/东的纬度和经度以及南/西的纬度和经度。

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool){
    print("The \(mapView.centerCoordinate)")
    print("the maprect \(mapView.visibleMapRect.origin)")
    print("the maprect \(mapView.visibleMapRect.size)")
    //I find just this attributes for mapView

    }

我不知道如何从我的数据中得到视口。我在网上找不到任何东西。
目的是我用谷歌分析和另一个工具评估数据的坐标日志。
有人有办法吗?多谢

mlnl4t2r

mlnl4t2r1#

Map工具包

let northEast = mapView.convertPoint(CGPoint(x: mapView.bounds.width, y: 0), toCoordinateFromView: mapView)
let southWest = mapView.convertPoint(CGPoint(x: 0, y: mapView.bounds.height), toCoordinateFromView: mapView)

谷歌Map

mapView有一个属性“projection”,您可以在其中使用“coordinateForPoint”将一个点从mapView转换为一个坐标。因此,以下操作可以正常工作:

用于谷歌Map的swift 3

let northEast = mapView.projection.coordinate(for: CGPoint(x: mapView.layer.frame.width, y: 0))
let southWest = mapView.projection.coordinate(for: CGPoint(x: 0, y: mapView.layer.frame.height))

雨燕2

let northEast = mapView.projection.coordinateForPoint(CGPoint(x: mapWidth, y: 0 ))
let southWest = mapView.projection.coordinateForPoint(CGPoint(x: 0, y: mapHeight))

只需输入mapView的宽度和高度

oxiaedzo

oxiaedzo2#

基于fel1xw answer

Swift 3Map工具包

let northEast = mapView.convert(CGPoint(x: mapView.bounds.width, y: 0), toCoordinateFrom: mapView)
let southWest = mapView.convert(CGPoint(x: 0, y: mapView.bounds.height), toCoordinateFrom: mapView)

相关问题