Reputation: 2847
My database has various points of interest. I want the user to see them based on their location. Also there are 3 buttons, which shows the the point of interests within radii of 2km / 5km / 15km. I cannot implement the zooming in functionality to these radii. So I am looking for a relationship between the zoom factor (from 2 to 21) and physical distance (in km).
I came across this piece of code on another question in SO.
private int calculateZoomLevel(int screenWidth) {
double equatorLength = 40075004; // in meters
double widthInPixels = screenWidth;
double metersPerPixel = equatorLength / 256;
int zoomLevel = 1;
while ((metersPerPixel * widthInPixels) > 2000) {
metersPerPixel /= 2;
++zoomLevel;
}
Log.i("ADNAN", "zoom level = "+zoomLevel);
return zoomLevel;
}
However this was used for google maps v1 and I'm afraid things might have changed since then. I can do map.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 15f));
to zoom in to my location with a zoom factor of 15, but I dont know the radius of the map now.
Thanks
Upvotes: 1
Views: 4206
Reputation: 2530
The easiest way to achieve your goal is to create a LatLngBound, which contains 4 points in the desired distance from the center, one moved to west, one to south, one to east and one to north.
builder = new LatLngBounds.Builder();
builder.include(latLng15KmToWest);
builder.include(latLng15KmToEast);
builder.include(latLng15KmToSouth);
builder.include(latLng15KmToNorth);
latLngBounds = builder.build();
Then you can use this LatLngBounds to create a CameraUpdate.
cameraUpdate = CameraUpdateFactory.newLatLngBounds(latLngBound, 0);
map.animateCamery(cameraUpdate);
The map will be zoomed exactly the size, that all 4 points will fit.
Unfortunately there is no way to create a CameraUpdate based on LatLngBounds, which keeps the bearing. It will always result in a north-up map. If that is o.k. in your case, you do not need to calculate the zoom level on your own. Otherwise see my other answer.
Upvotes: 1
Reputation: 2530
You can get the current screen width in km using the VisibleRegion (GoogleMap.getProjection().getVisibleRegion()) of your map and calculating the distance between the nearLeft and nearRight coordinate. This will give you the extent of the maps lower edge. It depends a bit on what you mean with "radius", whether this is accurate enough.
The zoom factors should still behave the same in GoogleMap API V2: An increment of 1 should double the distance. (I did not check, but I am concluding that from how the number of tiles the whole earth is mapped to depends on the zoom level: It is 4 ^ zoomlevel because the number of tiles doubles in both, x and y dimension, with each zoomlevel.)
Upvotes: 2