Offline map not working #577

Closed
TizioFittizio opened this Issue Apr 2, 2017 · 13 comments

Comments

Projects
None yet
3 participants
@TizioFittizio

Although I have seen dozens of threads about it, its seems that i can't run a simple application using an offline map

My map is created throught the MapTilePackager:
cattura

The fields are filled with the bounding box of Italy which i taken from the https://github.com/osmdroid/osmdroid/tree/master/OSMMapTilePackager read.me

This appear to me to create correctly my mapnik.zip archive with zoom from 3 to 8
The archive obtained is this: https://www.dropbox.com/s/zowz0nzic4969bo/Mapnik.ZIP?dl=0

Then i push the zip through the adb with the following commands:
adb push C:\Users\pc\Documents\mappe\Mapnik.zip /mnt/sdcard/osmdroid
When doing this i first check that osmdroid folder exists, after this command, if i inspect the sdcard with the shell the zip seems to be in the correct position following this page:
https://github.com/osmdroid/osmdroid/wiki/Offline-Map-Tiles

After that, i try to execute my application, which code is the following:

`public class MainActivity extends AppCompatActivity {

private final static int PERMISSION_CODE = 111;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (askPermission()) showMap();
}

private boolean askPermission(){
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
       ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CODE);
        return false;
    }
    else return true;
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_CODE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                showMap();
            }
            else {
                System.exit(0);
            }
        }
    }
}

private void showMap(){
    setContentView(R.layout.activity_main);
    MapView map = (MapView) findViewById(R.id.map);
    map.setUseDataConnection(false);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    map.setTilesScaledToDpi(true);
    IMapController controller = map.getController();
    GeoPoint startPoint = new GeoPoint(46.071067, 13.234579);
    controller.setCenter(startPoint);
    Marker startMarker = new Marker(map);
    startMarker.setPosition(startPoint);
    startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(startMarker);
    controller.setZoom(6);
    Configuration.getInstance().setDebugMode(true);
    //Configuration.getInstance().setDebugMapView(true);
    //Configuration.getInstance().setDebugTileProviders(true);
}

}
`

This will result in seeing the gray grid map on the application, if i check if the zip is in the correct folder, in is there
This is my dependencies in my build.gradle
dependencies { compile 'com.android.support:appcompat-v7:23.1.1' compile 'org.osmdroid:osmdroid-android:5.6' compile 'org.slf4j:slf4j-android:1.7.12' compile fileTree(dir: 'libs', include: ['*.jar']) }

Things that i have tried:

  • Pushing the file in sd/card/osmdroid

  • Pushing the file in storage/0/emulated/osmdroid

  • Pushing the file in the tiles folder inside osmdroid

  • Enable debug mode which result in spamming the logcat with
    - 04-02 08:39:28.696 18734-18765/com.example.android.osmdroidofflinedemo D/OsmDroid: Tile doesn't exist: /x/y/z messages as i move and zoom the map, checking manually for the existence of x/y/z will result frequently in tiles that aren't in the zip, so i don't know if this is supposed to be normal

  • Change the format file from png to jpeg

  • Build the application on different devices emulated, with api 23 and api 19, where i never experience a permission error

  • I also tried to run the sample in the openStreeMapViewer sampleOfflineOnly, this will confirm that the zip exists but nothing is showed

This is the android studio project that i'm trying to show an offline map, i started the project writing inside this: https://github.com/johnjohndoe/OSMDroidOfflineDemo

My project:
https://www.dropbox.com/s/c24unco3o0bc091/OfflineMap.rar?dl=0

@spyhunter99

This comment has been minimized.

Show comment
Hide comment
@spyhunter99

spyhunter99 Apr 2, 2017

Collaborator

sounds like you have a race condition. If you're asking for permissions after the map has been created or in the same activity as the map, then you are asking for permission too late.

Option 1, ask for permissions in a different activity or fragment, after permission is granted, then load the map activity or fragment

Option 2, after permission is granted, use mapview.setMapTileProvider(new ....) I think it's BasicTileProvider. That will reinitialize the cache and rescan for offline archives.

Collaborator

spyhunter99 commented Apr 2, 2017

sounds like you have a race condition. If you're asking for permissions after the map has been created or in the same activity as the map, then you are asking for permission too late.

Option 1, ask for permissions in a different activity or fragment, after permission is granted, then load the map activity or fragment

Option 2, after permission is granted, use mapview.setMapTileProvider(new ....) I think it's BasicTileProvider. That will reinitialize the cache and rescan for offline archives.

@TizioFittizio

This comment has been minimized.

Show comment
Hide comment
@TizioFittizio

TizioFittizio Apr 2, 2017

I applied your suggestion but the problem seems to persists

Now this is the first activity loaded:
`public class PermissionActivity extends Activity {

private final static int PERMISSION_CODE = 111;

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_permission);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            goToMapActivity();
        }
    });
    button.setEnabled(askPermission());

}

private boolean askPermission(){
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CODE);
        return false;
    }
    else return true;
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_CODE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                button.setEnabled(true);
            }
            else {
                System.exit(0);
            }
        }
    }
}

public void goToMapActivity(){
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}

}
`

After pressing the button the activity of the map is loaded:

``public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MapView map = (MapView) findViewById(R.id.map);
    map.setUseDataConnection(false);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    map.setTilesScaledToDpi(true);
    IMapController controller = map.getController();
    GeoPoint startPoint = new GeoPoint(46.071067, 13.234579);
    controller.setCenter(startPoint);
    Marker startMarker = new Marker(map);
    startMarker.setPosition(startPoint);
    startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(startMarker);
    controller.setZoom(6);
    Configuration.getInstance().setDebugMode(true);
    //Configuration.getInstance().setDebugMapView(true);
    //Configuration.getInstance().setDebugTileProviders(true);
}

The zip file is always the same present in the same directory
This is my project with the changes:
https://www.dropbox.com/s/p1dv3haxpau9ifz/OSMDroidOfflineDemo-master.rar?dl=0

I applied your suggestion but the problem seems to persists

Now this is the first activity loaded:
`public class PermissionActivity extends Activity {

private final static int PERMISSION_CODE = 111;

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_permission);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            goToMapActivity();
        }
    });
    button.setEnabled(askPermission());

}

private boolean askPermission(){
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CODE);
        return false;
    }
    else return true;
}

public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_CODE: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                button.setEnabled(true);
            }
            else {
                System.exit(0);
            }
        }
    }
}

public void goToMapActivity(){
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
}

}
`

After pressing the button the activity of the map is loaded:

``public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MapView map = (MapView) findViewById(R.id.map);
    map.setUseDataConnection(false);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    map.setTilesScaledToDpi(true);
    IMapController controller = map.getController();
    GeoPoint startPoint = new GeoPoint(46.071067, 13.234579);
    controller.setCenter(startPoint);
    Marker startMarker = new Marker(map);
    startMarker.setPosition(startPoint);
    startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(startMarker);
    controller.setZoom(6);
    Configuration.getInstance().setDebugMode(true);
    //Configuration.getInstance().setDebugMapView(true);
    //Configuration.getInstance().setDebugTileProviders(true);
}

The zip file is always the same present in the same directory
This is my project with the changes:
https://www.dropbox.com/s/p1dv3haxpau9ifz/OSMDroidOfflineDemo-master.rar?dl=0

@spyhunter99

This comment has been minimized.

Show comment
Hide comment
@spyhunter99

spyhunter99 Apr 2, 2017

Collaborator

I downloaded the zip file from the dropbox link and looks strange, perhaps there's a bug in the swing tile cacher but there's an extra folder that has no file name in the zip.

Collaborator

spyhunter99 commented Apr 2, 2017

I downloaded the zip file from the dropbox link and looks strange, perhaps there's a bug in the swing tile cacher but there's an extra folder that has no file name in the zip.

@spyhunter99

This comment has been minimized.

Show comment
Hide comment
@spyhunter99

spyhunter99 Apr 2, 2017

Collaborator

You'll also want to check this

Configuration.getInstance().getOsmdroidTileCache().getAbsolutePath();
Configuraton.getInstance().getOsmdroidBasePath().getAbsolutePath();

and confirm the path is where you put your zip file.

In the mean time i'll look into the zip generation issue

Collaborator

spyhunter99 commented Apr 2, 2017

You'll also want to check this

Configuration.getInstance().getOsmdroidTileCache().getAbsolutePath();
Configuraton.getInstance().getOsmdroidBasePath().getAbsolutePath();

and confirm the path is where you put your zip file.

In the mean time i'll look into the zip generation issue

@TizioFittizio

This comment has been minimized.

Show comment
Hide comment
@TizioFittizio

TizioFittizio Apr 2, 2017

The paths are:
storage/0/emulated/osmdroid
storage/0/emulated/osmdroid/tiles

The Mapnik.zip file is stored in storage/0/emulated/osmdroid

I'm still trying to get work the openStreetMapViewer sampleOfflineOnly, this is confirming that the zip is located in the correct path (since it says "using Mapnik.zip") but the result is the same gray grid

I'm still using this archive: : https://www.dropbox.com/s/zowz0nzic4969bo/Mapnik.ZIP?dl=0
I tried with different archives, all generated with the mapTilePackager but this does't change the final product

TizioFittizio commented Apr 2, 2017

The paths are:
storage/0/emulated/osmdroid
storage/0/emulated/osmdroid/tiles

The Mapnik.zip file is stored in storage/0/emulated/osmdroid

I'm still trying to get work the openStreetMapViewer sampleOfflineOnly, this is confirming that the zip is located in the correct path (since it says "using Mapnik.zip") but the result is the same gray grid

I'm still using this archive: : https://www.dropbox.com/s/zowz0nzic4969bo/Mapnik.ZIP?dl=0
I tried with different archives, all generated with the mapTilePackager but this does't change the final product

@niroj08

This comment has been minimized.

Show comment
Hide comment
@niroj08

niroj08 Apr 4, 2017

D/OsmDroid: Archives - Tile doesn't exist: /12/654/1583
Finding the way out .. hope to get a solution :/

niroj08 commented Apr 4, 2017

D/OsmDroid: Archives - Tile doesn't exist: /12/654/1583
Finding the way out .. hope to get a solution :/

@niroj08

This comment has been minimized.

Show comment
Hide comment
@niroj08

niroj08 Apr 4, 2017

D/OsmDroid: Archives - Tile doesn't exist: /12/654/1583 which is actually not downloaded.
But the mapView is looking for those folders.

Also the offline mapview dosent seem to work with me aswell ...

niroj08 commented Apr 4, 2017

D/OsmDroid: Archives - Tile doesn't exist: /12/654/1583 which is actually not downloaded.
But the mapView is looking for those folders.

Also the offline mapview dosent seem to work with me aswell ...

@spyhunter99

This comment has been minimized.

Show comment
Hide comment
@spyhunter99

spyhunter99 Apr 5, 2017

Collaborator

@TizioFittizio that zip has the same problem as the last one, did you also use the osmdroid tool to package it?

Collaborator

spyhunter99 commented Apr 5, 2017

@TizioFittizio that zip has the same problem as the last one, did you also use the osmdroid tool to package it?

@spyhunter99

This comment has been minimized.

Show comment
Hide comment
@spyhunter99

spyhunter99 Apr 5, 2017

Collaborator

@TizioFittizio did you run the swing user interface or from the command line? what os did you run it from?

Collaborator

spyhunter99 commented Apr 5, 2017

@TizioFittizio did you run the swing user interface or from the command line? what os did you run it from?

spyhunter99 added a commit that referenced this issue Apr 5, 2017

#577 minor fix for windows users using the map tile packager that ca…
…used the generation of zip files with an empty named sub folder
@TizioFittizio

This comment has been minimized.

Show comment
Hide comment
@TizioFittizio

TizioFittizio Apr 6, 2017

Its seems that after rebuilding the mapTilePackager and starting a new project form scratch i manage to get it work, maybe your fix has did the trick.

I put here a simple demo that work with an offline map, in order to help others who has my problem like @niroj08: https://github.com/TizioFittizio/OsmDroidOfflineMap

Anyway @spyhunter99 i was using the swing interface on my windows 10 with java 1.8, it happen sometimes that after downloaded all the tiles the zipping phase seem stucked, if i will have serious problem with that i will open another issue, thanks for your patience and for your help

Its seems that after rebuilding the mapTilePackager and starting a new project form scratch i manage to get it work, maybe your fix has did the trick.

I put here a simple demo that work with an offline map, in order to help others who has my problem like @niroj08: https://github.com/TizioFittizio/OsmDroidOfflineMap

Anyway @spyhunter99 i was using the swing interface on my windows 10 with java 1.8, it happen sometimes that after downloaded all the tiles the zipping phase seem stucked, if i will have serious problem with that i will open another issue, thanks for your patience and for your help

@niroj08

This comment has been minimized.

Show comment
Hide comment
@niroj08

niroj08 Apr 6, 2017

@TizioFittizio where is the location of your Mapnik.zip ?

niroj08 commented Apr 6, 2017

@TizioFittizio where is the location of your Mapnik.zip ?

@TizioFittizio

This comment has been minimized.

Show comment
Hide comment
@TizioFittizio

TizioFittizio Apr 6, 2017

I didn't included as mine was about 600 mb, but you can easily create yours using the mapTilePackager with the -gui argument

I didn't included as mine was about 600 mb, but you can easily create yours using the mapTilePackager with the -gui argument

@niroj08

This comment has been minimized.

Show comment
Hide comment
@niroj08

niroj08 Apr 6, 2017

@TizioFittizio great .. I figured I was missing something that I shouldnot .. Got my solution too .. a blunder ignorance :D Thank you

niroj08 commented Apr 6, 2017

@TizioFittizio great .. I figured I was missing something that I shouldnot .. Got my solution too .. a blunder ignorance :D Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment