Skip to content
Browse files

bug/#1145 - using the new DirectRenderer class of mapsforge (#1152)

* bug/#1145 - using the new DirectRenderer class of mapsforge

That fixes the issue.

Not to be merged as is, because we still rely on a `SNAPSHOT` version of `mapsforge`.
As soon as `mapsforge` issues a new version, the `build.gradle` file must be edited and marginally also a comment in `MapsForgeTileSource` about the `mapsforge` version number.

Impacted files
* `osmdroid-mapsforge/build.gradle`: now using the latest version of `mapsforge`, that includes the new `DirectRenderer` class that I coded in `mapsforge`
* `MapsForgeTileSource`: used `DirectRenderer` instead of `DatabaseRenderer`

* Use of new MapsForge's DirectRenderer.TileRefresher

Impacted classes:
* `MapTileProviderBase`: created method `expireInMemoryCache`, in order to "expire" a MapsForge tile that needs to be refreshed due to new overlapping labels from new neighboring tiles
* `MapsForgeTileProvider`: added a call to a tile refresher callback whenever a tile needs to be refreshed
* `MapsForgeTileSource`: method `renderTile` now returns `null` when MapsForge doesn't know the tile - used to be a yellow bitmap instead; created method `addTileRefresher`

* Impact of new mapsforge version

* edited the build.gradle according to the new mapsforge version number
* marginally edited a comment in MapsForgeTileSource

* #1145 fixes crashing due to out of bounds coordindates

* bug/#1145 - fine-tuning latitude 90/-90 side effect

Impacted class:
* `MapsForgeTileSource`: fine-tuned method `getBoundsOsmdroid` about the latitude 90/-90 side effect
  • Loading branch information...
monsieurtanuki authored and spyhunter99 committed Sep 26, 2019
1 parent a4a4c74 commit d80cc67285b3d0187cfbfe442574d99b1ed5cb2e
@@ -496,4 +496,17 @@ protected void computeTile(final long pMapTileIndex, final int pX, final int pY)
* @return the number of tile requests currently in the queue
public abstract long getQueueSize();

* Expire a tile that is in the memory cache
* Typical use is for mapsforge, where the contents of the tile can evolve,
* depending on the neighboring tiles that have been displayed so far.
* @since 6.0.3
public void expireInMemoryCache(final long pMapTileIndex) {
final Drawable drawable = mTileCache.getMapTile(pMapTileIndex);
if (drawable != null) {
ExpirableBitmapDrawable.setState(drawable, ExpirableBitmapDrawable.EXPIRED);
@@ -37,9 +37,9 @@ dependencies {
testCompile "junit:junit:${'junit.version')}"

//Mapsforge rendering and database support, which is LGPL
compile 'org.mapsforge:mapsforge-map-android:0.10.0'
compile 'org.mapsforge:mapsforge-map:0.10.0'
compile 'org.mapsforge:mapsforge-themes:0.10.0'
compile 'org.mapsforge:mapsforge-map-android:0.11.0'
compile 'org.mapsforge:mapsforge-map:0.11.0'
compile 'org.mapsforge:mapsforge-themes:0.11.0'

//osmdroid which is ASF licensed
compile project(':osmdroid-android')
@@ -3,13 +3,16 @@

import android.os.Build;

import org.mapsforge.core.model.Tile;
import org.osmdroid.tileprovider.IRegisterReceiver;
import org.osmdroid.tileprovider.MapTileProviderArray;
import org.osmdroid.tileprovider.modules.IFilesystemCache;
import org.osmdroid.tileprovider.modules.MapTileFileArchiveProvider;
import org.osmdroid.tileprovider.modules.MapTileFilesystemProvider;
import org.osmdroid.tileprovider.modules.SqlTileWriter;
import org.osmdroid.tileprovider.modules.TileWriter;
import org.osmdroid.util.MapTileIndex;

* This lets you hook up multiple MapsForge files, it will render to the screen the first
@@ -54,6 +57,14 @@ public MapsForgeTileProvider(IRegisterReceiver pRegisterReceiver, MapsForgeTileS
// is defined by the superclass.

// In mapsforge the tiles bitmap may need to be refreshed according to neighboring tiles' labels
pTileSource.addTileRefresher(new DirectRenderer.TileRefresher() {
public void refresh(final Tile pTile) {
final long index = MapTileIndex.getTileIndex(pTile.zoomLevel, pTile.tileX, pTile.tileY);

@@ -1,8 +1,6 @@
package org.osmdroid.mapsforge;

import android.util.Log;
@@ -12,20 +10,18 @@
import org.osmdroid.api.IMapView;
import org.osmdroid.tileprovider.tilesource.BitmapTileSourceBase;
import org.osmdroid.util.MapTileIndex;
import org.osmdroid.views.MapView;


@@ -46,7 +42,7 @@
private final float scale = DisplayModel.getDefaultUserScaleFactor();
private RenderThemeFuture theme = null;
private XmlRenderTheme mXmlRenderTheme = null;
private DatabaseRenderer renderer;
private DirectRenderer renderer;
private HillsRenderConfig hillsRenderConfig;

private MultiMapDataStore mapDatabase;
@@ -81,9 +77,11 @@ protected MapsForgeTileSource(String cacheTileSourceName, int minZoom, int maxZo
//renderer = new DatabaseRenderer(mapDatabase, AndroidGraphicFactory.INSTANCE, tileCache,
// new TileBasedLabelStore(tileCache.getCapacityFirstLevel()), true, true);
// mapsforge0.8.0
InMemoryTileCache tileCache = new InMemoryTileCache(2);
renderer = new DatabaseRenderer(mapDatabase, AndroidGraphicFactory.INSTANCE, tileCache,
new TileBasedLabelStore(tileCache.getCapacityFirstLevel()), true, true, hillsRenderConfig);
//InMemoryTileCache tileCache = new InMemoryTileCache(2);
//renderer = new DatabaseRenderer(mapDatabase, AndroidGraphicFactory.INSTANCE, tileCache,
// new TileBasedLabelStore(tileCache.getCapacityFirstLevel()), true, true, hillsRenderConfig);
// mapsforge0.11.0
renderer = new DirectRenderer(mapDatabase, AndroidGraphicFactory.INSTANCE, true, hillsRenderConfig);

minZoom = MIN_ZOOM;
maxZoom = renderer.getZoomLevelMax();
@@ -106,9 +104,11 @@ public BoundingBox getBounds(){

public org.osmdroid.util.BoundingBox getBoundsOsmdroid(){
BoundingBox boundingBox = mapDatabase.boundingBox();
org.osmdroid.util.BoundingBox bounds = new org.osmdroid.util.BoundingBox(boundingBox.maxLatitude, boundingBox.maxLongitude,
boundingBox.minLatitude, boundingBox.minLongitude);
return bounds;
final double latNorth= Math.min(MapView.getTileSystem().getMaxLatitude(),boundingBox.maxLatitude);
final double latSouth= Math.max(MapView.getTileSystem().getMinLatitude(),boundingBox.minLatitude);
return new org.osmdroid.util.BoundingBox(
latNorth, boundingBox.maxLongitude,
latSouth, boundingBox.minLongitude);

@@ -200,10 +200,7 @@ public synchronized Drawable renderTile(final long pMapTileIndex) {
} catch (Exception ex) {
Log.d(IMapView.LOGTAG, "###################### Mapsforge tile generation failed", ex);
//Make the bad tile easy to spot
Bitmap bitmap = Bitmap.createBitmap(TILE_SIZE_PIXELS, TILE_SIZE_PIXELS, Bitmap.Config.RGB_565);
return new BitmapDrawable(bitmap);
return null;

public static void createInstance(Application app) {
@@ -219,4 +216,13 @@ public void dispose() {

* @since 6.0.3
public void addTileRefresher(DirectRenderer.TileRefresher pDirectTileRefresher) {
if (pDirectTileRefresher != null) {

0 comments on commit d80cc67

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