0
使用Android Google Maps v2更改縮放後,我正在統計地圖上可見標記的數量。首先,我創建了CameraPosition如下:getVisibleRegion座標略有偏差
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中,但在邊界的.0001內。
我猜測報告的可見區域略大於實際。