在使用 Android Google Maps v2 更改缩放后,我正在计算地图上可见标记的数量。首先,我按如下方式创建 CameraPosition:
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(xx.xxxxxx, -xx.xxxxxx))
.zoom((float) 14.3)
.bearing(45)
.tilt(45)
.build();
然后我使用以下方法计算可见区域中的标记数:
googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
if (cameraPosition.zoom != priorZoom) {
priorZoom = cameraPosition.zoom;
LatLngBounds mLatLngBounds = googleMap.getProjection().getVisibleRegion().latLngBounds;
// get count of items in visible location
// retrieve coordinates of all locations from database
String[] selection = new String[] {String.valueOf(mLatLngBounds.southwest.latitude),
String.valueOf(mLatLngBounds.northeast.latitude),
String.valueOf(mLatLngBounds.southwest.longitude),
String.valueOf(mLatLngBounds.northeast.longitude)};
Cursor cursor = readingDb.rawQuery("select count(*) from ZLOCATION where (ZLATITUDE BETWEEN ? AND ?) AND (ZLONGITUDE BETWEEN ? AND ?)", selection);
cursor.moveToFirst();
int numVisible = cursor.getInt(0);
cursor.close();
Toast.makeText(getActivity(), "number visible markers is " + numVisible, Toast.LENGTH_LONG).show();
虽然显示的标记数接近实际数字,但我发现计数的标记数多于显示的数。当我查看包含但未显示的标记的坐标时,我发现它们的实际纬度和经度包含在 mLatLngBounds 中,但在边界的 0.0001 范围内。
我猜测报告的可见区域略大于实际区域。