查找 mapView 中显示的标记

Find markers shown in mapView

我正在尝试确定当前显示在地图视图中的 marker/s。我研究了以下方法:

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

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

如何转换标记的位置以被 contains 接受?

在 MapKit 中:

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

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

只有当标记的坐标可见时(换句话说,标记正在显示),才会打印"Found"。如果在屏幕外,它将打印 "Not found".

使用地图视图的当前 projection。使用称为 containsCoordinate 的投影方法来检查标记的位置是否在投影内,即当前可见。

所以像这样:

let coord = marker.position
let isVisible = self.mapview.projection.containsCoordinate(coord)

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