Skip to content
jaakla edited this page Oct 13, 2014 · 3 revisions

Quick guide

Clone this project git clone https://github.com/nutiteq/hellomap3d.git , import HelloMap3D project, compile it using Android SDK and run it on Android device (not emulator). It has basic map viewing and required 3rd party libraries included.

You can use Eclipse with Android SDK, Android Studio or another IDE - steps are the same.

Expected result: http://youtu.be/WzqpEBjx6jg

Step-by-step guide

This is how you can add map to your own app.

1. Prerequisities

  • Install Nutiteq SDK to your project - see Downloads for different options
  • Make sure your AndroidManifest.XML defines permission
<uses-permission android:name="android.permission.INTERNET"/>

2. Define your application main layout as res/layout/main.xml, so it has map element:

<?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" >
   <com.nutiteq.MapView
    android:id="@+id/mapView"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    />
</LinearLayout>

3. Create map object. Now we can define MapView type of member in your main activity class, load layout and load the MapView from layout. The object is created now.

public class HelloMap3DActivity extends Activity {

    private MapView mapView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.mapView);
...

4. Initialize and start map. Map object itself does not work right away. Three steps are needed here as minimum: (a) define map configuration package, which is put in Components object, (b) define base map layer and finally (c) tell map object to start map activities (downloading threads etc).

      // define new configuration holder object
      mapView.setComponents(new Components());

      // Define base layer. Here we use MapQuest open tiles which are free to use
      // Almost all online maps use EPSG3857 projection.
        // We use online data source for the tiles and the URL is given as template. 
        RasterDataSource dataSource = new HTTPRasterDataSource(new EPSG3857(), 0, 18, "http://otile1.mqcdn.com/tiles/1.0.0/osm/{zoom}/{x}/{y}.png");
        RasterLayer mapLayer = new RasterLayer(dataSource, 0);
        mapView.getLayers().setBaseLayer(mapLayer);
    }

    // it is suggested to start and stop mapping in Activity lifecycle methods, as following:

    @Override
    protected void onStart() {
        super.onStart();
        //Start the map - mandatory
        mapView.startMapping();
    }

    @Override
    protected void onStop() {
        //Stop the map - mandatory to avoid problems with app restart
        mapView.stopMapping();
        super.onStop();
    }

After this, if you are lucky, you can start the application on your phone and it should show map. Congratulations!

Clone this wiki locally