Integrating MapsForge into an Android project

MapsForge is an Android library that allows you to display Offline detailed maps. Installation instructions are available on its official website, as well as good samples.  This tutorial explains how to integrate it inside your Android App once it is installed.

create a new Android project, and open it. Mine will be named “PrjOfflineMaps”. You can also use a project you already have.

Eclipse IDE

On the Package Explorer, right click on project root  > Properties

on the left list, click on “Java Build path”, and mark the “Libraries” tab.

Now click on “Add external JARs”, and point to the hidden folder where maven has installed the repo:

Windows

“C:\Users\user_name\.m2\repository\org\mapsforge\mapsforge-map.3.1-SNAPSHOT”

Linux

Haven’t tried it on Linux, but there has to be a similar folder inside your home directory.

Now click on tab “Order and Export”, and make sure “mapsforge-map-0.3.1-SNAPSHOT-jar-with-dependencies.jar” is checked.

Now create a new Activity that extends “MapActivity” (org.mapsforge.android.maps.MapActivity).

public class MyMapActivity extends MapActivity{
    private static final File MAP_FILE = new File(Environment.getExternalStorageDirectory().getPath(), "mymap.map");
     @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       MapView mapView = new MapView(this);
       mapView.setClickable(true);
       mapView.setBuiltInZoomControls(true);
       FileOpenResult fileOpenResult = mapView.setMapFile(MAP_FILE);
       if (!fileOpenResult.isSuccess()) {
         Toast.makeText(this, fileOpenResult.getErrorMessage(), Toast.LENGTH_LONG).show();
         finish();
       }
       setContentView(mapView);
    }
 }

Now declare the recently created activity in the AndroidManifest.xml, inside <application> tag:

    <application>
        ...
        <activity android:name=".offline.MyMapActivity" />
    </application>

When starting a "MyMapActivity" instance, you should see the map. for more info about creating your own .map files, see this link. Hope it helps!
Advertisements

2 thoughts on “Integrating MapsForge into an Android project

  1. What do you mean? You mean centering the map?
    You can call setCenter() anytime you want to move the map center. I can’t remember the parameters now, but there is a Lat,Lon parameter, and another one to enable a movement animation when centering.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s