Search code examples
androidmapsopenstreetmaposmdroid

OSM maps look scrambled on android


I am trying to load OpenStreetMaps on an android app but the map does not load correctly. They are displayed unordered.

I am using a self-hosted map server which loads correctly in a browser (Used leaflet for the demo).

The code as follows,

public class MapActivity extends AppCompatActivity {

    final private int MIN_ZOOM_LEVEL = 3;
    final private int MAX_ZOOM_LEVEL = 18;
    final private int TILE_SIZE = 256;
    final private String IMAGE_EXTENSION = ".png";

    MapView map = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //load/initialize the osmdroid configuration
        Context ctx = getApplicationContext();
        Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));

        setContentView(R.layout.activity_map);
        map = (MapView) findViewById(R.id.map);

        map.setTilesScaledToDpi(true);
        map.setMultiTouchControls(true);


        map.setTileSource(new OnlineTileSourceBase("Tiles", 2, 18, 256, ".png",
                new String[] { "http://maps.MY-DOMAIN.com/tile/" }) {
            @Override
            public String getTileURLString(long pMapTileIndex) {

                String url = getBaseUrl()
                        + MapTileIndex.getZoom(pMapTileIndex)
                        + "/" + MapTileIndex.getX(pMapTileIndex)
                        + "/" + MapTileIndex.getY(pMapTileIndex)
                        + mImageFilenameEnding;

                Log.e("MAP",url);
                return url;
            }
        });
    }
}

What I end up getting is,

enter image description here

How can I solve this issue? I have taken a look into the documentation but nothing is mentioned about this.


Solution

  • Either the tile source is z/x/y vs z/y/x OR the y coordinate is inverted the tile source is closer to the TMS specification vs the slippy map format that open street maps uses. In either case, correct the issue, clear the tile cache, then try it again