Andrew Wong
Andrew Wong

Reputation: 49

OSMDROID - Zoom to show all markers

This is a duplicate of questions #1339, #156 on GitHub but I wasn't able to find them helpful

I have an app where it displays two points on a map I've implemented that the markers are set correctly, but how can I zoom the map, so all markers are visible? If I have only one marker, it's quite easy as I know the location and set a fixed zoom.

I've tried using BoundingBox, gathering the maximum and min lat/lon values for all points, but I run into this error :

Process: com.example.cslapp, PID: 9216
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cslapp/com.example.cslapp.MapRoute}: java.lang.IllegalArgumentException: north must be in [-85.05112877980658,85.05112877980658]
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3492)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3652)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2099)

I also tried using

BoundingBox box = BoundingBox.fromGeoPoints(waypoints);
map.zoomToBoundingBox(box, true);

but this resulted in the same error.

Any suggestions or solutions would be great, thanks!

Upvotes: 2

Views: 773

Answers (1)

Basti X
Basti X

Reputation: 81

Running into this issue for weeks now. The strange thing is: in some fragments I constantly get this error and in some never. Some people say it is because the layout has not been inflated yet but I am not sure if that is really the case.

One workaround that seems to help is running the zoom function inside a Runnable:

mapView.post {
            mapView.zoomToBoundingBox(boundingBox, true, 100)
        }

Update: So there really does seem to be an inflation issue because in the case of your Exception getWidth() returns 0. My suggestion would be to call the zooming function at a later time. And to prevent App crashes a simple check if(width>0) is really all that is needed.

Upvotes: 5

Related Questions