mapsforge and osmdroid

975 views
Skip to first unread message

Ahmed Madhun

unread,
Feb 14, 2018, 4:36:53 AM2/14/18
to mapsforge-dev
Hey,

i am developing an android project that uses openstreetmap offline. I tried to download tails using OSMTilePackeger but it took long time.
I was searching for a better a way to implement it, and some where was suggested mapsforge.

can i just use mapsforge that is included in osmdroid 5.6.5 ? any tutorial to help ?
or just use the normal mapsforge library in github ?

Emux

unread,
Feb 14, 2018, 4:44:43 AM2/14/18
to mapsfo...@googlegroups.com
(don't know Osmdroid integration)

To work properly with Mapsforge offline vector maps can use:

- Mapsforge vector map library

- VTM OpenGL vector map library (with advanced features)

There are sample apps and many examples in each repository.

--
Emux

Ahmed Madhun

unread,
Feb 14, 2018, 5:24:33 AM2/14/18
to mapsforge-dev
Hey, again ... thanks for answering.

is there any demo or tutorials, that shows how to use it ?

Emux

unread,
Feb 14, 2018, 5:27:08 AM2/14/18
to mapsfo...@googlegroups.com
Mapsforge has a getting started guide and a collection of examples in mapsforge-samples-android app.

VTM has a collection of samples in vtm-android-example app.

--
Emux

Ahmed Madhun

unread,
Feb 15, 2018, 6:00:54 AM2/15/18
to mapsforge-dev
A question again please .... where should i store the .map file ?
I tried to use the adb to push it to the device but i had 2 errors:
1- File do not exist
2- Can't read the file

What could the problem be ?

Emux

unread,
Feb 15, 2018, 6:10:07 AM2/15/18
to mapsfo...@googlegroups.com
The map is a regular file and can be stored in any internal or external storage as offered by Android (see documentation).

Then if the needed permissions are requested can load it inside the app with exact same way.

Getting started guide offers an example.

--
Emux

Ahmed Madhun

unread,
Feb 16, 2018, 5:47:16 AM2/16/18
to mapsforge-dev
I actually tried the Getting started guide to see how it works ... the app works without any error but the map doesn't displays, however the maptools is displayed (like zooming buttons)

Here is a copy of my code:

package com.example.a7med.osmforgetest;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.android.util.AndroidUtil;
import org.mapsforge.map.android.view.MapView;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;

import java.io.File;

public class MainActivity extends AppCompatActivity {

// name of the map file in the external storage
private static final String MAP_FILE = "norway.map";
// name of the map file in the external storage
private static final int REQUEST_READ = 123;
private MapView mapView;

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

// App Initialization
AndroidGraphicFactory.createInstance(this.getApplication());

checkMapPremission();
}

public void checkMapPremission()
{
// Check if the premission is already avalible
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
showMap();

}
else
{
/*
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE))
{
Toast.makeText(this, "Permission is needed", Toast.LENGTH_LONG).show();
}

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ);
*/

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ);
}
}

public void showMap()
{
// Create the map
this.mapView = new MapView(this);
setContentView(this.mapView);

this.mapView.setClickable(true);
this.mapView.getMapScaleBar().setVisible(true);
this.mapView.setBuiltInZoomControls(true);
this.mapView.setZoomLevelMin((byte) 0);
this.mapView.setZoomLevelMax((byte) 20);

// create a tile cache of suitable size
TileCache tileCache = AndroidUtil.createTileCache(this, "mapcache",
mapView.getModel().displayModel.getTileSize(), 1f,
this.mapView.getModel().frameBufferModel.getOverdrawFactor());

MapDataStore mapDataStore = new MapFile(new File(Environment.getExternalStorageDirectory(), MAP_FILE));
TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
this.mapView.getModel().mapViewPosition, AndroidGraphicFactory.INSTANCE);
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);

// only once a layer is associated with a mapView the rendering starts
this.mapView.getLayerManager().getLayers().add(tileRendererLayer);

//this.mapView.setCenter(new LatLong(52.517037, 13.38886));
this.mapView.setZoomLevel((byte) 12);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
switch (requestCode)
{
case REQUEST_READ:
{
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
showMap();
}
else
{
//code for deny
Toast.makeText(this, "Denyed ...", Toast.LENGTH_LONG).show();
}
break;
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

@Override
protected void onDestroy()
{
this.mapView.destroyAll();
AndroidGraphicFactory.clearResourceMemoryCache();
super.onDestroy();
}
}
 
Do you see any error that is reasonable  for not displaying the map ?

Emux

unread,
Feb 16, 2018, 5:57:10 AM2/16/18
to mapsfo...@googlegroups.com
Better to start with simple steps.
Like implement the Getting started guide in a simple app with target sdk 22 to overcome runtime permissions and see if all run correctly.

Runtime permissions are irrelevant to a map library, they must be handled in app level by developers according to Android documentation.

Also make sure to place the map file to whatever path the Environment.getExternalStorageDirectory() returns for your device / emulator environment, usually it's internal storage root.

--
Emux
Reply all
Reply to author
Forward
0 new messages