calculate zoom level for the full route points, to fit whole route perfectly to the view of map(screen)

1,136 views
Skip to first unread message

L K

unread,
Dec 30, 2012, 9:01:26 AM12/30/12
to mapsfo...@googlegroups.com
Hello,

How can I calculate the zoom level for the full route to fit perfectly to the view of map(screen)?
I determinated the minLat, minLon and maxLat maxLon positions, and created a BoundingBox with these calculated values, I can get the CenterPoint of BoundingBox, and positioning to the center of my route.
But how can I calculate the zoom level, for fit whole route to the screen?

thanks in advance

hannes....@googlemail.com

unread,
Dec 30, 2012, 2:55:29 PM12/30/12
to mapsfo...@googlegroups.com
I use something like this to get the zoom level for a bounding box:
dx = width of bbox projected to zoom_level 0 / tile_size (should
result in range 0..1)
zoom_x = floor(-log(4)*log(dx) + screen_width/tile_size)
new_zoom = min(zoom_x,zoom_y)

BR

> thanks in advance
>
> --
>
>

L K

unread,
Dec 30, 2012, 6:30:37 PM12/30/12
to mapsfo...@googlegroups.com, hannes....@googlemail.com

Hello

I don't understand perfectly.
How can I get dx?
Where comes the value of zoom_y from?

Thank you of your response,
LK

Martin Pearman

unread,
Dec 31, 2012, 4:18:41 AM12/31/12
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Here's some code i pieced together to create a new MapView method named fitToBoundingBox:

    public synchronized void fitToBoundingBox(final BoundingBox pBoundingBox, final int pMaximumZoom) {
        int width = this.getWidth();
        int height = this.getHeight();
        if (width <= 0 || height <= 0) {
            this.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    MapView.this.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    fitToBoundingBox(pBoundingBox, pMaximumZoom);
                }
            });
        } else {
            Projection projection1 = this.getProjection();
            GeoPoint pointSouthWest = new GeoPoint(pBoundingBox.minLatitude, pBoundingBox.minLongitude);
            GeoPoint pointNorthEast = new GeoPoint(pBoundingBox.maxLatitude, pBoundingBox.maxLongitude);
            Point pointSW = new Point();
            Point pointNE = new Point();
            byte maxLvl = (byte) Math.min(pMaximumZoom, this.getMapZoomControls().getZoomLevelMax());
            byte zoomLevel = 0;
            while (zoomLevel < maxLvl) {
                byte tmpZoomLevel = (byte) (zoomLevel + 1);
                projection1.toPoint(pointSouthWest, pointSW, tmpZoomLevel);
                projection1.toPoint(pointNorthEast, pointNE, tmpZoomLevel);
                if (pointNE.x - pointSW.x > width) {
                    break;
                }
                if (pointSW.y - pointNE.y > height) {
                    break;
                }
                zoomLevel = tmpZoomLevel;
            }
            this.getMapViewPosition().setMapPosition(new MapPosition(pBoundingBox.getCenterPoint(), zoomLevel));
        }
    }

The check for width and height being zero is optional - the MapView has no layout when initially created/added to your Activity so this code waits until it has layout.
You could omit that if not required.

This method works on MapsForge version 0.3.1, if you're using an older version you'll need to modify it slighty for the different syntax in your version.

Martin.

L K

unread,
Dec 31, 2012, 6:12:59 AM12/31/12
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Hannes,

Thank you! It works perfectly!

LK.


2012. december 30., vasárnap 20:55:29 UTC+1 időpontban hannes....@gmail.com a következőt írta:

L K

unread,
Dec 31, 2012, 6:14:03 AM12/31/12
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Hello Martin,

I will try this solution as soon as possible.

Thank you of your response!

Christopher Stephan

unread,
Jun 27, 2013, 4:53:17 AM6/27/13
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Hey,

I am trying to implement the method fitToBoundingBox() with mapsforge 0.3.0 but find no way to set the MapPosition to the mapView. Martin do you know which syntax changes are involved??

mapView.getMapPosition()..... setMapPosition(new MapPosition(pBoundingBox.getCenterPoint(), zoomLevel));

Martin Pearman

unread,
Jul 1, 2013, 2:40:39 AM7/1/13
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Look at the MapView getController() method.

Use getController().setZoom() first and then getController().setCenter().

Martin.
Message has been deleted

Chen Lee

unread,
Jul 4, 2013, 12:57:35 AM7/4/13
to mapsfo...@googlegroups.com

Can anyone please tell me where to implement the method fittoBoundingBox()?
I am implementing this method in main java file but i guess I am going wrong. 
Looking forward to a prompt response

Christian Pesch

unread,
Oct 24, 2013, 10:45:41 AM10/24/13
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Thank you for the hint. The following code works nicely for me for the mapsforge-rewrite branch, which I'm using in RouteConverter to experiment with offline maps:

  private void zoomToBounds(BoundingBox boundingBox) {
        Dimension mapViewDimension = mapView.getModel().mapViewDimension.getDimension();
        if(mapViewDimension == null)
            return;
        double dxMax = longitudeToPixelX(boundingBox.maxLongitude, (byte) 0) / TILE_SIZE;
        double dxMin = longitudeToPixelX(boundingBox.minLongitude, (byte) 0) / TILE_SIZE;
        double zoomX = floor(-log(3.8) * log(abs(dxMax-dxMin)) + mapViewDimension.width / TILE_SIZE);
        double dyMax = latitudeToPixelY(boundingBox.maxLatitude, (byte) 0) / TILE_SIZE;
        double dyMin = latitudeToPixelY(boundingBox.minLatitude, (byte) 0) / TILE_SIZE;
        double zoomY = floor(-log(3.8) * log(abs(dyMax-dyMin)) + mapViewDimension.height / TILE_SIZE);
        int newZoom = new Double(min(zoomX, zoomY)).intValue();
        setZoom(newZoom);
    }

HTH

L K

unread,
Mar 8, 2014, 11:02:16 AM3/8/14
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Hi Martin,

Where can I download jar of MapsForge version 0.3.1?

Thanks


2012. december 31., hétfő 10:18:41 UTC+1 időpontban Martin Pearman a következőt írta:

emux

unread,
Mar 8, 2014, 11:23:36 AM3/8/14
to mapsfo...@googlegroups.com, hannes....@googlemail.com
0.3.1 jar does not exist, you can get its source.

But 0.3.X versions are not maintained anymore and lack certain features.

It's recommended to advance to rescue branch where development continues
and eventually will be released as 0.4.0 version.
It's quite stable and feature complete.

You can find its source at main repository and jars at Jenkins.
Also check its Samples app for various usage examples.

Best regards, Emux
Cruiser - Cruiser Beta
Atlas - Atlas Beta

hardik jain

unread,
May 18, 2015, 5:40:45 AM5/18/15
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Hello,

Thanks for sharing this code. I tried this code but every time it set maximum zoom level. So even if overlays are scattered all over it is only showing one overlay.

Emux

unread,
May 18, 2015, 5:45:06 AM5/18/15
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Try LatLongUtils.zoomForBounds(dimension, boundingBox, tileSize).

You can find its use in ZoomToBounds example in Samples.

--
Emux
Cruiser - Atlas

hardik jain

unread,
May 18, 2015, 7:52:51 AM5/18/15
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Hello, I am using Mapsforge 0.3.0.
mMapView.getModel is not available in the MapView class.
Can you tell my why this code brings me to highest zoom level possible in the map?
I want to Zoom Out the map so that I can view all map point over the map.

Thanks.

Emux

unread,
May 18, 2015, 8:04:55 AM5/18/15
to mapsfo...@googlegroups.com, hannes....@googlemail.com
Are you using a bounding box with the elements min/max coordinates?

Also you can see LatLongUtils.zoomForBounds implementation.

BTW I recommend advancing to latest Mapsforge release, those are really old versions, a lot has changed since then.

Reply all
Reply to author
Forward
0 new messages