Calculating zoom level to fit a bounding box

1,342 views
Skip to first unread message

George

unread,
Mar 6, 2012, 5:52:21 AM3/6/12
to mapsforge-dev
Hello dear mapsforge developers,

I have a bunch of markers which need to be ALL displayed on a map.

Ideally, I'd like to calculate a zoom level in which all markers to be
displayed are visible, similar to Google Maps' fitBounds method
(http://code.google.com/apis/maps/documentation/javascript/
reference.html#Map)

How can this be achieved?

Thank you,
George

Thilo Mühlberg

unread,
Mar 8, 2012, 3:38:45 PM3/8/12
to mapsfo...@googlegroups.com
Hello George,

there is no such method in the mapsforge map library yet. You should
open a new issue to track this feature request. Maybe somebody comes up
with a patch before we eventually find the time to implement a solution.

Regards,
Thilo

signature.asc

sylvek

unread,
Mar 9, 2012, 12:23:48 PM3/9/12
to mapsforge-dev
found somewhere on the mapsforge issue.

public static boolean zoomAndPan(MapView mapView, int minLatE6, int
maxLatE6, int minLngE6, int maxLngE6)
{
int width = mapView.getWidth();
int heigth = mapView.getHeight();
if (width <= 0 || heigth <= 0) {
return false;
}
int cntLat = (maxLatE6 + minLatE6) / 2;
int cntLng = (maxLngE6 + minLngE6) / 2;

mapView.getController().setCenter(new GeoPoint(cntLat,
cntLng));

GeoPoint pointSouthWest = new GeoPoint(minLatE6, minLngE6);
GeoPoint pointNorthEast = new GeoPoint(maxLatE6, maxLngE6);

Projection projection = mapView.getProjection();
Point pointSW = new Point();
Point pointNE = new Point();
byte maxLvl = (byte)
mapView.getMapGenerator().getZoomLevelMax();
byte zoomLevel = 0;
for (; zoomLevel < maxLvl;) {
byte tmpZoomLevel = (byte) (zoomLevel + 1);
projection.toPoint(pointSouthWest, pointSW, tmpZoomLevel);
projection.toPoint(pointNorthEast, pointNE, tmpZoomLevel);
if (pointNE.x - pointSW.x > width) {
break;
}
if (pointSW.y - pointNE.y > heigth) {
break;
}
zoomLevel = tmpZoomLevel;
}
mapView.getController().setZoom(zoomLevel);
return true;
>  signature.asc
> < 1KViewDownload
Reply all
Reply to author
Forward
0 new messages