Android: How do I set the zoom level of map view to 1 km radius around my current location?
The following code is what ended up using. Given the screen width and the fact that at zoom level 1 the equator of Earth is 256 pixels long and every subsequent zoom level doubles the number of pixels needed to represent earths equator, the following function returns the zoom level where the screen will show an area of 2Km width.
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;
}
Zooming Google map to specific radius in miles in android
I got the solution for the same, Here is the one:
// Zoom in, animating the camera.
double iMeter = iMiles * 1609.34;
circle.remove();
circle = gooMap.addCircle(new CircleOptions()
.center(new LatLng(selectedLat, selectedLong))
.radius(iMeter) // Converting Miles into Meters...
.strokeColor(Color.RED)
.strokeWidth(5));
circle.isVisible();
float currentZoomLevel = getZoomLevel(circle);
float animateZomm = currentZoomLevel + 5;
Log.e("Zoom Level:", currentZoomLevel + "");
Log.e("Zoom Level Animate:", animateZomm + "");
gooMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(selectedLat, selectedLong), animateZomm));
gooMap.animateCamera(CameraUpdateFactory.zoomTo(currentZoomLevel), 2000, null);
Log.e("Circle Lat Long:", selectedLat + ", " + selectedLong);
And our method that calculate the zoom level as per device is as follows:
public float getZoomLevel(Circle circle) {
float zoomLevel=0;
if (circle != null){
double radius = circle.getRadius();
double scale = radius / 500;
zoomLevel =(int) (16 - Math.log(scale) / Math.log(2));
}
return zoomLevel +.5f;
}
Android - Compute distance of map every zoom level like Gmaps scale bar
You can find the radius between right/left most point and center of the map when map becomes idle by using
/**
* Method used to fetch Map radius
*/
private int getMapRadius() {
LatLng latlng = googleMap.getProjection().getVisibleRegion().latLngBounds.getCenter();
if (latLng == null)
return 0;
LatLng latLng1 = googleMap.getProjection().getVisibleRegion().farRight;
return (int) MapUtils.computeDistance(latLng1.latitude, latLng1.longitude, latLng.latitude, latLng.longitude);
}
Convert Google Map zoom level into km
You can calculate the distance between the center of the map and the top left coordinate like this:
VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion();
double distance = SphericalUtil.computeDistanceBetween(
visibleRegion.farLeft, mMap.getCameraPosition().target);
Note that I'm using the SphericalUtil.computeDistanceBetween
method from the Google Maps Android API Utility Library.
Related Topics
Determining If an Android Device Is Rooted Programmatically
How to Display a Route Between Two Geocoords in Google Maps
How to Give Hexagon Shape to Imageview
Android Edittext Onchange Listener
Android Slidingdrawer from Top
Android:Fill Spinner from Java Code Programmatically
How to Enable Standard Copy Paste for a Textview in Android
Adding Local .Aar Files to My Gradle Build
Hiding Title in a Fullscreen Mode
Extract Code Country from Phone Number [Libphonenumber]
List All the Files from All the Folder in a Single List
How to Get Child of Child Value from Firebase in Android
How to Execute the Dex File in Android with Command
How to Wrap the Height of a Viewpager to the Height of Its Current Fragment
Searchview's Oncloselistener Doesn't Work
How to Display Activity When the Screen Is Locked
How to Change Bitmap Image Color in Android
Recyclerview Gridlayoutmanager: How to Auto-Detect Span Count