Skip to content
Permalink
Browse files

Merge pull request #1417 from osmdroid/bug/#1416

bug/#1416 - additional zoom check before tile download
  • Loading branch information...
monsieurtanuki committed Oct 9, 2019
2 parents 94ff394 + d0afbd8 commit 2aab5a93a2645a918f746296ac316b15f4a55a6e
@@ -94,6 +94,7 @@
import org.osmdroid.samplefragments.tileproviders.SampleAssetsOnlyRepetitionModes;
import org.osmdroid.samplefragments.tileproviders.SampleOfflineGemfOnly;
import org.osmdroid.samplefragments.tileproviders.SampleOfflineOnly;
import org.osmdroid.samplefragments.tileproviders.SampleUnreachableOnlineTiles;
import org.osmdroid.samplefragments.tileproviders.SampleVeryHighZoomLevel;
import org.osmdroid.samplefragments.tilesources.SampleBingHybrid;
import org.osmdroid.samplefragments.tilesources.SampleBingRoad;
@@ -315,6 +316,7 @@ private SampleFactory() {
mSamples.add(SampleSpeechBalloon.class);
mSamples.add(SampleMapCenterOffset.class);
mSamples.add(SampleSnappable.class);
mSamples.add(SampleUnreachableOnlineTiles.class);
}

public void addSample(Class<? extends BaseSampleFragment> clz) {
@@ -0,0 +1,54 @@
package org.osmdroid.samplefragments.tileproviders;

import org.osmdroid.samplefragments.BaseSampleFragment;
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.tileprovider.tilesource.TileSourcePolicy;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
import org.osmdroid.util.GeoPoint;

/**
* Demo checking if the zoom restriction for tiles is correctly applied
* We actually download MAPNIK tiles but only for zoom levels 14 and 15
* @author Fabrice Fontaine
* @since 6.1.3
*/
public class SampleUnreachableOnlineTiles extends BaseSampleFragment {

private static final int ZOOM_MIN = 14;
private static final int ZOOM_MAX = 15;

/**
* cf. {@link TileSourceFactory#MAPNIK}
*/
private static final OnlineTileSourceBase MAPNIK_FOR_TESTS = new XYTileSource("Mapnik",
ZOOM_MIN, ZOOM_MAX, 256, ".png", new String[] {
"https://a.tile.openstreetmap.org/",
"https://b.tile.openstreetmap.org/",
"https://c.tile.openstreetmap.org/" },"© OpenStreetMap contributors",
new TileSourcePolicy(2,
TileSourcePolicy.FLAG_NO_BULK
| TileSourcePolicy.FLAG_NO_PREVENTIVE
| TileSourcePolicy.FLAG_USER_AGENT_MEANINGFUL
| TileSourcePolicy.FLAG_USER_AGENT_NORMALIZED
));

@Override
public String getSampleTitle() {
return "Zoom Restricted Online Tiles (" + ZOOM_MIN + "-" + ZOOM_MAX + ")";
}

@Override
protected void addOverlays() {
super.addOverlays();

mMapView.setTileSource(MAPNIK_FOR_TESTS);
mMapView.post(new Runnable() {
@Override
public void run() {
mMapView.getController().setZoom(ZOOM_MIN * 1f);
mMapView.setExpectedCenter(new GeoPoint(45.7597, 4.8422)); // Lyon, France
}
});
}
}
@@ -122,7 +122,7 @@ private void search(final long pMapTileIndex) {
}
}
}
final Drawable drawable = provider.getTileLoader().loadTile(pMapTileIndex);
final Drawable drawable = provider.getTileLoader().loadTileIfReachable(pMapTileIndex);
if (drawable == null) {
continue;
}
@@ -180,7 +180,7 @@ public static Bitmap approximateTileFromLowerZoom(
MapTileIndex.getX(pMapTileIndex) >> pZoomDiff,
MapTileIndex.getY(pMapTileIndex) >> pZoomDiff);
try {
final Drawable srcDrawable = pProvider.getTileLoader().loadTile(srcTile);
final Drawable srcDrawable = pProvider.getTileLoader().loadTileIfReachable(srcTile);
if (!(srcDrawable instanceof BitmapDrawable)) {
return null;
}
@@ -71,6 +71,14 @@
*/
public abstract int getMaximumZoomLevel();

/**
* @since 6.1.3
*/
public boolean isTileReachable(final long pMapTileIndex) {
final int zoom = MapTileIndex.getZoom(pMapTileIndex);
return zoom >= getMinimumZoomLevel() && zoom <= getMaximumZoomLevel();
}

/**
* Sets the tile source for this tile provider.
*
@@ -184,7 +192,8 @@ protected void removeTileFromQueues(final long pMapTileIndex) {
public abstract class TileLoader implements Runnable {

/**
* Load the requested tile.
* Actual load of the requested tile.
* Do implement this method, but call {@link #loadTileIfReachable(long)} instead
*
* @since 6.0.0
* @return the tile if it was loaded successfully, or null if failed to
@@ -194,10 +203,21 @@ protected void removeTileFromQueues(final long pMapTileIndex) {
public abstract Drawable loadTile(final long pMapTileIndex)
throws CantContinueException;

/**
* @since 6.1.3
*/
public Drawable loadTileIfReachable(final long pMapTileIndex)
throws CantContinueException {
if (!isTileReachable(pMapTileIndex)) {
return null;
}
return loadTile(pMapTileIndex);
}

@Deprecated
protected Drawable loadTile(MapTileRequestState pState)
throws CantContinueException {
return loadTile(pState.getMapTile());
return loadTileIfReachable(pState.getMapTile());
}

protected void onTileLoaderInit() {
@@ -309,7 +329,7 @@ final public void run() {
}
try {
result = null;
result = loadTile(state.getMapTile());
result = loadTileIfReachable(state.getMapTile());
} catch (final CantContinueException e) {
Log.i(IMapView.LOGTAG,"Tile loader can't continue: " + MapTileIndex.toString(state.getMapTile()), e);
clearQueue();

0 comments on commit 2aab5a9

Please sign in to comment.
You can’t perform that action at this time.