Search code examples
osmdroid

Offline OSMdroid shows empty map


I am trying to use the offline mode of OSMdroid in an android.support.v4.app.Fragment. My tiles are donwloaded by MOBAC using the "OSMdroid Zip" format around the coordinate 0,0 of the globe (it's just sea, but I just want something to show up as a beginning).

My zip file is locates in /sdcard/osmdroid/MapquestOSM.zip, and contains the folder MapQuest, which contains himself all the tiles in subfolders with numbers as names, from zoom 11 to zoom 16. The tiles are png files and have the extension ".png". Note that the /sdcard//osmdroid folder only contains my zip file.

My problem is that I see only an empty map, which is an empty grey grid.

public class ContactMapFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.map_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);


        MapView map = (MapView) getActivity().findViewById(R.id.mapview);
        map.setBuiltInZoomControls(true);
        map.getController().setZoom(11);

        map.getController().setCenter(new GeoPoint(0,0));

        map.setClickable(true);
        map.setUseDataConnection(false);
        map.setMaxZoomLevel(16);
        map.setMinZoomLevel(11);

        map.setTileSource(new XYTileSource("MapQuest",
                ResourceProxy.string.offline_mode, 11, 16, 256, ".jpg", new String[] {
                "http://otile1.mqcdn.com/tiles/1.0.0/map/",
                "http://otile2.mqcdn.com/tiles/1.0.0/map/",
                "http://otile3.mqcdn.com/tiles/1.0.0/map/",
                "http://otile4.mqcdn.com/tiles/1.0.0/map/"})); 
    }
}

Using osmdroid-android-4.2 and slf4j-api-1.7.10 Permissions are :

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>

So I have two questions : Why is nothing displayed ? What are the urls in the XYTileSource supposed to point to (I found these urls on a tutorial) ?


Solution

  • Here is the solution I have finally used, it's working. If I remember well, you don't have to care about the zip file name, although I'm not sure it's true.

    private void createMap(){
        map = (MapView) getActivity().findViewById(R.id.mapview);
    
        //first parameter indicates the folder inside the zip file containing the atlas.
        String atlasName = "MapQuest";
        String atlasExtension = ".png";
        int tileSizePixels = 256;
        int defaultLatitude = 400000000;
        int defaultLongitude = 20000000;
        int minZoom = 11;
        int maxZoom = 17;
        int defaultZoom = 14;
    
        map.setTileSource(new XYTileSource(atlasName, ResourceProxy.string.mapquest_osm, minZoom, maxZoom, tileSizePixels, atlasExtension, new String[] {}));
    
        map.setBuiltInZoomControls(true);
        map.getController().setZoom(defaultZoom);
        map.setClickable(true);
    
        map.getController().setCenter(new GeoPoint(defaultLatitude, defaultLongitude));
    }
    

    Inside the zip file are the tiles stored in a .png format.

    Hope it will help.