查找mapView中显示的标记

时间:2016-09-14 02:46:45

标签: swift google-maps-sdk-ios

我正在尝试确定当前在mapview中显示的标记。我研究了以下方法:

self.mapView.bounds.contains(markers[0].position)

但是contains命令接受CGPoint或CGRect。在除Swift之外的其他平台中,contains可以接受标记的位置。

如何将标记的位置转换为contains接受?

2 个答案:

答案 0 :(得分:8)

使用mapview的当前urlList。使用名为projection的投影方法检查您的标记位置是否在投影内,a.k.a。当前可见。

类似于:

containsCoordinate

https://developers.google.com/maps/documentation/ios-sdk/reference/interface_g_m_s_projection.html#aa6ad29e6831e40f00435c3aaeda8e08a

答案 1 :(得分:2)

在MapKit中

我认为Marker是指MKAnnotation。您应该使用mapView而不是使用visibleMapRect的范围,并查看它是否包含MKMapPoints中标记的坐标。这是我使用的代码:

let markerPoint = MKMapPointForCoordinate(markers[0].coordinate)
if MKMapRectContainsPoint(mapView.visibleMapRect, markerPoint) {
    print("Found")
} else {
    print("Not found")
}

只有当标记的坐标可见时(换句话说,正在显示标记),才会打印“找到”。如果屏幕外,它将打印“未找到”。