Search code examples
javaandroideclipseosmdroid

Creating an offline map on Android


I'm new to Android app development and I have to create an Offline map Android application.

I used Mobile Atlas Creator to get the map views osmdroid .zip format, and I don't know how to add it in my app.

Can someone show me how to use Osmdroid in my app? I would be grateful if you could provide step-by-step instructions.


Solution

  • This is an absolute minumum example project for Osmdroid which I made sometime ago.

    package osmdemo.demo;
    
    import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
    import org.osmdroid.util.GeoPoint;
    import org.osmdroid.views.MapController;
    import org.osmdroid.views.MapView;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    // This is all you need to display an OSM map using osmdroid
    public class OsmdroidDemoMap extends Activity {
    
        private MapView         mMapView;
        private MapController   mMapController;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.osm_main);
            mMapView = (MapView) findViewById(R.id.mapview);
            mMapView.setTileSource(TileSourceFactory.MAPNIK);
            mMapView.setBuiltInZoomControls(true);
            mMapController = mMapView.getController();
            mMapController.setZoom(13);
            GeoPoint gPt = new GeoPoint(51500000, -150000);
            //Centre map near to Hyde Park Corner, London
            mMapController.setCenter(gPt);
        }
    }
    

    Have this in your osm_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <org.osmdroid.views.MapView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:id="@+id/mapview" />
    </LinearLayout>
    

    Include slf4j-android-1.5.8.jar and osmdroid-android-3.0.5.jar in the build path. (Google search for where to get them from)