在Android的Google Maps v2中,我该如何获取可见的标记?我知道可以使用投影并消除点 < 0 和点 > 屏幕大小。但是我不想逐个检查,如果我有很多标记,这可能会太慢。是否有简单的方法?或者一些现成的解决方案?如果有,那么是哪一个?
好的,以下是我以前使用过的代码,用于确定用户可以看到什么,然后只绘制可见的标记。 我认为您可能能够将其适应于您的目的。
获取地图的当前矩形“视口”(注意:必须在主线程上运行)
this.mLatLngBounds = this.mMap.getProjection().getVisibleRegion().latLngBounds;
将两个点(左上角和右下角)排序,以便我们可以使用最小/最大逻辑。
double lowLat;
double lowLng;
double highLat;
double highLng;
if (this.mLatLngBounds.northeast.latitude < this.mLatLngBounds.southwest.latitude)
{
lowLat = this.mLatLngBounds.northeast.latitude;
highLat = this.mLatLngBounds.southwest.latitude;
}
else
{
highLat = this.mLatLngBounds.northeast.latitude;
lowLat = this.mLatLngBounds.southwest.latitude;
}
if (this.mLatLngBounds.northeast.longitude < this.mLatLngBounds.southwest.longitude)
{
lowLng = this.mLatLngBounds.northeast.longitude;
highLng = this.mLatLngBounds.southwest.longitude;
}
else
{
highLng = this.mLatLngBounds.northeast.longitude;
lowLng = this.mLatLngBounds.southwest.longitude;
}
那么在我的情况下,我有这些数据存储在数据库中,因此我可以使用 >= 和 <= 来仅提取我想要的针脚。
fun GoogleMap.isMarkerVisible(markerPosition: LatLng) =
projection.visibleRegion.latLngBounds.contains(markerPosition)
private GoogleMap mMap;
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Hello world"));
// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0))
.add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude
.add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west
.add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south
.add(new LatLng(37.35, -122.0)); // Closes the polyline.
// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);