MrWeezle
MrWeezle

Reputation: 21

Use two osmdroid Map Views in one Activity

I want to use two Map Fragments, each with a different fragment, side-by-side in an activity. The left map is planned to show a overview over a specific area, the right one zooms in to a specific point.

The problem I'm having is, that only the first (left) map fragment is being correctly shown, the second (right) one shows a world map: Screenshot

Zooming is not working on the right map.

Here is a part of the code I'm using:

private void initializeMap()
{       
    MapTileProviderBasic tileProviderOSM = new MapTileProviderBasic(getApplicationContext());
    ITileSource tileSourceOSM = new XYTileSource("OpenFireMap", null, 1, 18, 256, ".png", new String[]{"http://openfiremap.org/hytiles/"});
    tileProviderOSM.setTileSource(tileSourceOSM);
    TilesOverlay tilesOverlayOSM = new TilesOverlay(tileProviderOSM, this.getBaseContext());
    tilesOverlayOSM.setLoadingBackgroundColor(Color.TRANSPARENT);

    OsmFragment frag = (OsmFragment) getFragmentManager().findFragmentById(R.id.osmMap);
    osmMap = frag.getMap();
    osmMap.setMultiTouchControls(true);
    osmMap.getOverlays().add(tilesOverlayOSM);

    MapTileProviderBasic tileProviderOSMDetail = new MapTileProviderBasic(getApplicationContext());
    ITileSource tileSourceOSMDetail = new XYTileSource("OpenFireMap", null, 1, 18, 256, ".png", new String[]{"http://openfiremap.org/hytiles/"});
    tileProviderOSMDetail.setTileSource(tileSourceOSMDetail);
    TilesOverlay tilesOverlayOSMDetail = new TilesOverlay(tileProviderOSMDetail, this.getBaseContext());
    tilesOverlayOSMDetail.setLoadingBackgroundColor(Color.TRANSPARENT);

    OsmFragment fragDetail = (OsmFragment) getFragmentManager().findFragmentById(R.id.osmMapDetail);
    osmMapDetail = fragDetail.getMap();
    osmMapDetail.setMultiTouchControls(true);
    osmMapDetail.getOverlays().add(tilesOverlayOSMDetail);
}

If I change either one of the fragments to a GoogleMaps fragment, the osmdroid map loads correctly.

Any ideas why this is happening and how I can fix it? I'm using the newest osmdroid version from gradle (v5.6.4)

Thanks in advance.

EDIT: I changed the variable declation, but it still doesn't work.

Upvotes: 1

Views: 262

Answers (2)

MrWeezle
MrWeezle

Reputation: 21

So I found the solution. The problem was, that the MapView is within a fragment and the fragment is a custom class which had the hardcoded ID's and layouts in it. The solution which worked for me was duplicating the custom fragment class and editing the ID and Layout. Then in MainActivity I had to change the varaible declaration and the cast from OsmFragment to OsmFragmentDetail Now both maps are showing the wanted content.

Upvotes: 0

Okas
Okas

Reputation: 2674

You have to create separate TilesOverlays for different map views. Currently you are using the single overlay in two fragments, this can not work.

Upvotes: 0

Related Questions