将GMSMapView上的位置转换为屏幕的坐标系

时间:2017-08-03 09:45:05

标签: ios google-maps-sdk-ios coordinate-systems

enter image description here

我正在尝试将红色引脚的位置从GMSMapView转换为其superview的坐标系(UIViewController的视图)。

基本上,从附图中可以看出,相对于GMSMapView,引脚的位置是(170,275)。但是,我无法使用GoogleMaps SDK提供的功能来获得该值。

这是我尝试过的: 第1步,我有针脚的GPS坐标,我使用:

将其转换为CGPoint
let pinLocation = mapView.projection.point(for: pinCoordinate)

得到的pinLocation具有巨大的幅度(例如:(1614979.75,-1187222.25)),它与mapView中的可见引脚不对应。 第2步,我在不同视图的坐标系之间转换点的位置:

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文档和本网站类似问题的研究后,我找不到任何问题的答案。

1 个答案:

答案 0 :(得分:1)

将CGPoint转换为经时长

aaa this is line 1
bbb this is line 2
ccc this is line 3

将长时间转换为CGPoint

let topLeft = CGPoint(x: 0, y: 100)
let firstSideLatLong = mapView.projection.coordinate(for: topLeft)