Issue
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?
Solution
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;
}
Answered By - binW
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.