Alex Beardsley - 1 year ago 84

Java Question

I know what the input and outputs are, but I'm just not sure how or why it works.

This code is being used to, given a min and max longitude/latitude (a square) that contains a set of points, determine the maximum zoom level on Google Maps that will still display all of those points. The original author is gone, so I'm not sure what some of these numbers are even for (i.e. 6371 and 8). Consider it a puzzle =D

`int mapdisplay = 322; //min of height and width of element which contains the map`

double dist = (6371 * Math.acos(Math.sin(min_lat / 57.2958) * Math.sin(max_lat / 57.2958) +

(Math.cos(min_lat / 57.2958) * Math.cos(max_lat / 57.2958) * Math.cos((max_lon / 57.2958) - (min_lon / 57.2958)))));

double zoom = Math.floor(8 - Math.log(1.6446 * dist / Math.sqrt(2 * (mapdisplay * mapdisplay))) / Math.log (2));

if(numPoints == 1 || ((min_lat == max_lat)&&(min_lon == max_lon))){

zoom = 11;

}

Answer

Some numbers can be explained easily

MeanRadiusEarthInKm = 6371 (according to IUGG)

DegToRadDivisor = 180/PI = 57.2958

And again the zoom level doubles the size with each step, i.e. increase the zoomlevel by one halfs the size on the screen.

```
zoom = 8 - log(factor * dist) / log(2) = 8 - log_2(factor * dist)
=> dist = 2^(8-zoom) / factor
```

From the numbers we find that zoom level eight corresponds to a distance of 276.89km.