我正在尝试将红色落针的位置从 GMSMapView 转换为其 super View (UIViewController 的 View )的坐标系。
基本上,正如您从附图中看到的那样,图钉相对于 GMSMapView 的位置是 (170,275)。但是,我无法使用 GoogleMaps SDK 提供的功能来获取该值。
这是我尝试过的: 第 1 步,我获得了图钉的 GPS 坐标,并使用以下方法将其转换为 CGPoint:
let pinLocation = mapView.projection.point(for: pinCoordinate)
生成的 pinLocation 具有巨大的量值(例如:(1614979.75, -1187222.25)),它与 mapView 中的可见 pin 不对应。 步骤2,我在不同 View 的坐标系之间转换点的位置:
let pinLocationInView = mapView.convert(pinLocation, to: view) // view contains the mapView
并且pinLocationInView的大小几乎与pinLocation相同。显然,这种方法并没有达到我的预期。
然后,我尝试获取mapView投影的可见区域,希望通过简单的数学运算得到结果:
let region = projection.visibleRegion()
let farLeft = projection.point(for: region.farLeft)
let xOffset = pinLocation.x - farLeft.x
let yOffset = pinLocation.y - farLeft.y
令我惊讶的是,它也不起作用。原因是可见区域不是我在屏幕上实际看到的,farLeft是(0.0, 0.0)
在对适用于 iOS 的 GoogleMaps SDK 文档以及本网站中的类似问题进行了大量研究之后,我找不到任何问题的答案。
最佳答案
将 CGPoint 转换为经纬度
let topLeft = CGPoint(x: 0, y: 100)
let firstSideLatLong = mapView.projection.coordinate(for: topLeft)
将经纬度转换为 CGPoint
let coordinate = CLLocation(latitude:40.712216, longitude:-74.22655)
let point = mapView.projection.point(for: coordinate)
关于ios - 将 GMSMapView 上的位置转换为屏幕坐标系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45480568/