iosandroiddev / osmdroid

Automatically exported from code.google.com/p/osmdroid
0 stars 0 forks source link

Constantly loading offline tiles #544

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi everyone. Using 4.2 osmdroid.
As of recently started experiencing strange behavior.
MapView is placed inside ViewPagers Fragment. I'm using a custom offline source 
to supply tiles.

public class OfflineTileSource extends BitmapTileSourceBase {

    //  private final int                           cacheSize   = 4 * 1024 * 1024;
    private final LruCache<String, Drawable>    cache   = new LruCache<String, Drawable>(64);

    public OfflineTileSource() {
        super("MapQuest", ResourceProxy.string.mapquest_osm, 11, 18, 256, ".jpg");
    }

    @Override
    public String getTileRelativeFilenameString(final MapTile tile) {
        String src = String.format(Locale.getDefault(), "%s/%d/%d/%d%s", pathBase(), tile.getZoomLevel(), tile.getX(),
                tile.getY(), imageFilenameEnding());
        //      Log.v(OfflineTileSource.class.getName(), "Requesting: " + src);
        return src;
    }

    @Override
    public Drawable getDrawable(String arg0) {
        Log.v(OfflineTileSource.class.getName(), "GetDrawable: " + arg0);
        //      new Throwable().printStackTrace();

        Drawable d = cache.get(arg0);
        if (d != null)
            return d;

        d = super.getDrawable(arg0);
        cache.put(arg0, d);
        return d;
    }
}

Tiles are being set by:
mapView.setTileSource(new OfflineTileSource());

Data connection disabled:
mapView.setUseDataConnection(false);

After starting to scroll
public Drawable getDrawable(String arg0)
is being called thousands of times / second and does not stop.

If you do not override getDrawable and cache them, there are constant GC work 
that slows all.

Also, scrolling another fragment seems to trigger map loading also.

What can be the problem?

Original issue reported on code.google.com by oleg.top...@gmail.com on 12 Aug 2014 at 7:17

GoogleCodeExporter commented 8 years ago
looking at code I've found strange code parts:
// Check to see if file has expired
                    final long now = System.currentTimeMillis();
                    final long lastModified = file.lastModified();
                    final boolean fileExpired = lastModified < now - mMaximumCachedFileAge;

                    if (fileExpired && drawable != null) {
                        if (DEBUGMODE) {
                            logger.debug("Tile expired: " + tile);
                        }
                        drawable.setState(new int[] {ExpirableBitmapDrawable.EXPIRED });
                    }

This piece of code is setting EXPIRED to all drawables, probably forcing them 
to be reloaded again and again.

Original comment by oleg.top...@gmail.com on 12 Aug 2014 at 7:28

GoogleCodeExporter commented 8 years ago
The code that you called strange is doing exactly what it's supposed to be 
doing.

I think the problem you're describing is that the expiration shouldn't apply 
for offline tile sources.

Please submit an issue in GitHub.

Original comment by neilboyd on 13 Aug 2014 at 12:55