5

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.

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
Adams
  • 105
  • 4
  • 10

1 Answers1

4

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)

JJD
  • 50,076
  • 60
  • 203
  • 339
NickT
  • 23,844
  • 11
  • 78
  • 121
  • thx for the answer can you tell me how to call the views extracted from Mobile Atlas Creator or it's automatic? – Adams Nov 23 '11 at 18:21
  • 1
    It's automatic, just drop the zip file into folder /sdcard/osmdroid on your phone – NickT Nov 23 '11 at 18:27
  • thx NickT it worked can you tell me just in the simulator in which folder do i put the pic .zip thx – Adams Nov 23 '11 at 22:06
  • 1
    Once you start the application in the emulator, if you go to the DDMS view, open the file explorer tab. There will be 3 folders visible - put the zip file in the 'sdcard' folder – NickT Nov 23 '11 at 23:18
  • i did so but it didn't work i have just an empty view no map pleeese help me. and can you tell me from where do we get the GeoPoint gPt = new GeoPoint(51500000, -150000); i mean the geopoint longitude and latitude – Adams Nov 24 '11 at 11:39
  • It sounds like the Mobac file you created doesn't cover the area round 51.5 degrees N, 0.15 degrees W which is the point in my example. (That's in central London). Either change those coords in your app to be inside your Mobac area, or download Central London in Mobac. If the area isn't in the zip file, then the app should get the tiles over the network, so I'm surprised your map is empty – NickT Nov 24 '11 at 17:15
  • i have found another code on the net that implements public class map extends Activity implements MapViewConstants and the app got the tiles over the net but it didn't open hte map that i got it on the .zip file i really don't know what to do??? – Adams Nov 24 '11 at 18:31
  • when i added the implementation to your app it worked but the app got the map over the network and it didn't open .zip file and i think that the problem is in my .zip because i want to get a map form Google earth, satellite images so i don't know if it works????? – Adams Nov 24 '11 at 19:31
  • You must have an old version of Mobac. The author was forced to withdraw map sources like Google earth in later versions on threat of legal action for copyright breach. In any case Osmdroid will only open zip files containing MAPNIK or other OSM sources like OSMRENDERER - hence the line in my example 'TileSourceFactory.MAPNIK' – NickT Nov 24 '11 at 19:58
  • is there an other way then Osmdroid to oepn Google map in a map app? because i don't see how to open it really? – Adams Nov 24 '11 at 20:33
  • Only if you write your own custom tile source code, but I've never done that. – NickT Nov 24 '11 at 23:50
  • hi nick can you tell me if wé can open image in mapview with having same map options – Adams Nov 27 '11 at 19:25