Igor Popov
Igor Popov

Reputation: 10101

How to set maximum zoom in osmdroid?

I already checked this answer, but the example does nothing else than show gray tiles (I'm in offline mode) for zoom level greater than the limit I give (4 in my case)...

OnlineTileSourceBase source = new XYTileSource("tiles", ResourceProxy.string.offline_mode, 0, 4, 256, ".png", "");

The following code shows maxZoom=4:

int zoomLevel = source.getMaximumZoomLevel();
Toast.makeText(this, "maxZoom=" + zoomLevel, Toast.LENGTH_SHORT).show();

For zoom levels between 0 and 4 my code works as expected: it loads map tiles from the SD card. My understanding of the problem is that the code shows all the tiles for each zoom it finds and when no other zooms are found it still zooms in.

The API clearly specifies setting the max zoom level in the constructor of XYTileSource (final int aZoomMaxLevel):

public XYTileSource(final String aName, final string aResourceId, final int aZoomMinLevel,
                        final int aZoomMaxLevel, final int aTileSizePixels, final String aImageFilenameEnding,
                        final String... aBaseUrl)

Any workarounds? What am I doing wrong? How can I block the zoom so that the user can't go beyond level 4?

Upvotes: 1

Views: 3783

Answers (3)

jatowler
jatowler

Reputation: 101

The MapView class defines the public function getMaxZoomLevel(). A trivial extension of MapView will let you override that function to return whatever you want without having to recompile the osmdroid source into a JAR:

public class ZoomLimitMapView extends MapView
{
    /* snip the constructors */

    @Override
    public int getMaxZoomLevel()
    {
        return 4;
    }
}

You don't need a minimum zoom level, but if you did, getMinZoomLevel() is also public and could be overridden. And obviously using the constant literal 4 is probably a bad idea; better to load dynamically from SharedPreferences unless you know your imagery will never, ever change.

Upvotes: 10

andrejc
andrejc

Reputation: 490

You can find detailed intruction how to build the jar here here

Changing MAXIMUM_ZOOMLEVEL in both org.osmdroid.views.util.constants.MapViewConstants and org.osmdroid.tileprovider.constants.OpenStreetMapTileProviderConstants should do the job.

Get the osmdroid source from SVN http://osmdroid.googlecode.com/svn/branches/release_3_0_5 I recomend this version cause it will be easier to eventualy apply the scroll limit patch beside the zoom limit.

Upvotes: 1

Dominik
Dominik

Reputation: 98

We found a workaround to set the maximum zoomlevel globally for the whole application.You have to change the value to your desired level in the file OpenStreetMapTileProviderConstants in the package org.osmdroid.tileprovider.constants in the osmdroid-android project. It´s not the proper way, but works quite fine for us! :)

Upvotes: 3

Related Questions