Search code examples
androidgoogle-maps-api-3openstreetmaposmdroid

How to use OSM map in an android application.? Is there any tutorial to learn about using OSM in android.?


I am searching for a tutorial/manual or steps to include Open street map into my android application. All I found is either a big project with lot more functionality on it, otherwise so many questions ended without proper conclusion about "HOW"..!

Is there any proper blog/site or document that can a fresher can refer?


Solution

  • I don't know of any tutorials but here's the code I wrote for a minimal example using Osmdroid.

    // This is all you need to display an OSM map using osmdroid
    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;
    
    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.DEFAULT_TILE_SOURCE);
            mMapView.setBuiltInZoomControls(true);
            mMapController = (MapController) mMapView.getController();
            mMapController.setZoom(13);
            GeoPoint gPt = new GeoPoint(51500000, -150000);
            mMapController.setCenter(gPt);
        }
    }
    /* HAVE THIS AS YOUR osm_main.xml
    ---------------------------------------------------------- XML START
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <org.osmdroid.views.MapView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/mapview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true" />
    </LinearLayout>
    ---------------------------------------------------------- XML END
    Include slf4j-android-1.5.8.jar and osmdroid-android-4.1.jar in the build path
    (Google search for where to get them from)
    */
    

    Note that you must now use the latest version (4.1) to avoid getting blocked from downloading tiles from OSM.

    Also note that they are moving their repositries to Github and the process isn't complete yet. This page downloads holds the links for the jars