Search code examples
javaandroidosmdroid

OSM map, custom tile source is not used


I have the following code:

public class MainActivity extends Activity {
    MapView map;
    XYTileSource customTileSource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = (MapView) findViewById(R.id.mapView);
        map.setMultiTouchControls(true);
        map.setMapListener(new MyMapListener());
        ITileSource tileSource = TileSourceFactory.MAPNIK;
        String tileUrl[] = {"https://bla_bla_bla__don't wanna show this here"};
        customTileSource = new MyXYTileSource("Point", null, 8, 18, 256, ".png", tileUrl);
        map.setTileSource(customTileSource);

        GeoPoint startPoint = new GeoPoint(47.021461, 28.86383);
        IMapController mapController = map.getController();
        mapController.setCenter(startPoint);
        mapController.setZoom(10);

        MapEventsOverlay eventsOverlay = new MapEventsOverlay(this, new MyMapEventsListener());
        map.getOverlays().add(eventsOverlay);

    }

    private class MyMapListener implements MapListener {

        @Override
        public boolean onScroll(ScrollEvent event) {
            return false;
        }

        @Override
        public boolean onZoom(ZoomEvent event) {
            Log.d("atf", "Zoom level is: " + event.getZoomLevel());
            return true;
        }
    }

    private class MyMapEventsListener implements MapEventsReceiver {

        @Override
        public boolean singleTapConfirmedHelper(GeoPoint p) {
            Log.d("atf", "Taped on long="+p.getLongitude()+"  lat="+p.getLatitude());
            return true;
        }

        @Override
        public boolean longPressHelper(GeoPoint p) {
            return false;
        }
    }

    private class MyXYTileSource extends XYTileSource {

        public MyXYTileSource(String aName, ResourceProxy.string aResourceId, int aZoomMinLevel, int aZoomMaxLevel, int aTileSizePixels, String aImageFilenameEnding, String[] aBaseUrl) {
            super(aName, aResourceId, aZoomMinLevel, aZoomMaxLevel, aTileSizePixels, aImageFilenameEnding, aBaseUrl);
        }

        @Override
        public String getTileURLString(MapTile aTile) {
            String tileUrl = super.getTileURLString(aTile);
            Log.d("atf", "Tile url String: "+tileUrl);
            return tileUrl;
        }
    }
}

The purpose of this is to use the custom tiles to display the map. This is actually working fine. Except for the case when this code runs on "Samsung galaxy Ace 3". Maybe there are other devices with same issue.

For zoom levels lower then 17, it doesn't load the custom tiles. It starts loading them when zoom goes above 17.

Can somebody please explain me why?

Sorry, I forgot to specify: When new tiles are not loaded, the old ones are loaded (from default tile source). So the map is not completely blank.


Solution

  • The solution was simpler than I though. It simply was because of caches. On the device specified in the question I have another Application which seems to use the same library "OSM Bonus Pack". Both applications where using the same directory from external storage for tile caching. I simply deleted that directory (/storage/emulated/0/osmdroid) and the new tiles started to load from my App.

    Perhaps a better solution is to specify another directory for my App for tile caching. Of course it will take some time until I find out how to do this, but I don't think that this will be complicated.