as referenced at http://laudontech.com/GISBlog/?p=28, I know the scale in meters for google map zoom levels:
Zoom level 0 1:20088000.56607700 meters
Zoom level 1 1:10044000.28303850 meters
Zoom level 2 1:5022000.14151925 meters
Zoom level 3 1:2511000.07075963 meters
Zoom level 4 1:1255500.03537981 meters
Zoom level 5 1:627750.01768991 meters
Zoom level 6 1:313875.00884495 meters
Zoom level 7 1:156937.50442248 meters
Zoom level 8 1:78468.75221124 meters
Zoom level 9 1:39234.37610562 meters
Zoom level 10 1:19617.18805281 meters
Zoom level 11 1:9808.59402640 meters
Zoom level 12 1:4909.29701320 meters
Zoom level 13 1:2452.14850660 meters
Zoom level 14 1:1226.07425330 meters
Zoom level 15 1:613.03712665 meters
Zoom level 16 1:306.51856332 meters
开发者_JAVA百科Zoom level 17 1:153.25928166 meters
Zoom level 18 1:76.62964083 meters
Zoom level 19 1:38.31482042 meters
I want to use this information to create a function like so:
function getZoomLevel(meters) {
//take input in whole meters
//return best match for zoom level as integer
}
I've thought about doing nested if else greater/less then conditions, but isn't there a more streamlined approach?
Each decrease of the zoom level means doubling the meters, so you don't have to store each individual value.
You could try this:
function getZoomLevel(m){
var z=0;
var i=20088000;
while(i/2>=m && z<19){
z++;
i=i/2;
}
return z;
}
it returns the next smaller zoom level as soon as meters is exceeding the current level.
or better:
function getZoomLevel(m){
var z=Math.floor((Math.log(20088000/m))/Math.log(2));
if (z>19) z=19;
if (z<0) z=0;
return z;
}
If you stored the zoom levels in an array, e.g.
var zoomLevels = [
20088000.56607700,
10044000.28303850,
5022000.14151925,
2511000.07075963
// etc. ...
];
Then iterate for a best fit. Because it is known that the zoom level array is decreasing, the search will be O(n)
.
function getZoomLevel(meters) {
for (var idx = 0; idx < zoomLevels.length; ++idx) {
if (zoomLevels[idx] < meters) {
return idx;
}
}
return zoomLevels.length - 1;
}
精彩评论