justik - 1 year ago 129

Java Question

In my Java application I am using JmapViewer; the map is drawn on the jPanel. Is it possible to get the map scale corresponding to the current zoom level? The map is based on the well-known Mercator projection:

`X = R * lon * cos(lat1);`

Y = R * log(tan(lat/2 + 45)), lat1 = 0;

The projection is conformal, so the local linear scale is independent of the direction. My idea uses two points sharing the same parallel:

`Point p1 = new Point(0,0);`

Point p2 = new Point(100,0);

final double dlon = getPosition(p2).getLon() - getPosition(p1).getLon();

final double scale = 6380000 * abs(dlon) * PI / 180 / 100; //scale = (R * dlon)/||p1-p2||

An improvement: the initial point p1 may be a center of the view.

`Point p1 = new Point(jPanel1.width()/2,jPanel1.height()/2);`

Point p2 = new Point(jPanel1.width()/2 + 100, jPanel1.height()/2);

The pixel coordinates are evaluated "in meters"; the formula lacks the pixel size...

Answer Source

The `JMapViewer`

method `getMeterPerPixel()`

returns the number of meters per pixel at the current zoom level. It uses `TileSource::getDistance`

, which "Gets the distance using the spherical law of cosines." In `org.openstreetmap.gui.jmapviewer.Demo`

, you can see how the value varies inversely with the zoom; level 10 and 98.5 meters/pixel are shown below. See `OsmMercator::getDistance`

for a typical implementation.