Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I set the zoom level of map view to 1 km radius around my current location?

I want to set the map view zoomed to 1km radius but cant figure out how?

The doc says that the zoom level 1 will map earths equator to 256 pixels. So how do I calculate which zoom level I need to set so that the map view shows area in 1KM radius?

UPDATE:
After reading a few blog posts I wrote the following code:

private int calculateZoomLevel() {     double equatorLength = 6378140; // 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; } 

The idea is that first I calculate Meters per pixel in the zoom level 1, which according to google shows equator of earth using 256 pixels. Now every subsequent zoom level magnifies by a level of 2 so I half the meters per pixel for every zoom level. I do this until I have a zoom level where meters per pixel multiplied by the screen width gives me less than 2000 i.e 2 Km across.

But I dont think that the zoom level I am getting is showing the map of 2Km radius. Can some one tell me what I am doing wrong here?

like image 581
binW Avatar asked May 14 '11 14:05

binW


People also ask

How do I show a radius on Google Maps Android?

Google Maps does not support the radius functionality, which means that you can't determine the radius around a given location. But you can measure the distance between two or more points. As a quick reminder, the radius of a circle is the distance from its edge to its center.

How do I zoom in smaller increments on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

How do you calculate zoom level on a map?

Convert latitude, longitude to spherical mercator x, y. Get distance between your two points in spherical mercator. The equator is about 40m meters long projected and tiles are 256 pixels wide, so the pixel length of that map at a given zoom level is about 256 * distance/40000000 * 2^zoom.


2 Answers

although this answer is logical and i find it working but the results are not accurate i dont know why but i tired this approach and this technique is far more accurate.

1) Make a circle on object with desired radius

Circle circle = mGoogleMap.addCircle(new CircleOptions().center(new LatLng(latitude, longitude)).radius(getRadiusInMeters()).strokeColor(Color.RED));                    circle.setVisible(true);         getZoomLevel(circle); 

2) Pass that object to this function and set the zoom level Here's a link

public int getZoomLevel(Circle circle) { if (circle != null){     double radius = circle.getRadius();     double scale = radius / 500;     zoomLevel =(int) (16 - Math.log(scale) / Math.log(2)); } return zoomLevel; } 
like image 117
Syed Raza Mehdi Avatar answered Sep 19 '22 20:09

Syed Raza Mehdi


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; } 
like image 29
binW Avatar answered Sep 18 '22 20:09

binW