Skip to content

Display a map

Famoco devices do not have Google Play Services installed by default, therefore it is not possible to use Google Maps to display maps into your apps. As an alternative, we recommend to use OpenStreetMap for Android which provides native support for Android. This page gives a step by step guide to integrate osmdroid library into your app. Full details are available on the official osmdroid Github projet here.

Step 1: Import the osmdroid library

Add the following dependency to your build.gradle file

dependencies {
    implementation 'org.osmdroid:osmdroid-android:(INSERT_VERSION_HERE):release@aar'
}

Step 2: Set the authorizations

Set the following authorizations in your AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 3: Set the layout

Create a src/main/res/layouts/main.xml layout like this one

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

Step 4: Main Activity

Now create the main activity (MainActivity.java):

import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.views.MapView;

public class MainActivity extends AppCompatActivity {
    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Context ctx = getApplicationContext();
        //important! set your user agent to prevent getting banned from the osm servers 
        Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
        setContentView(R.layout.activity_main);

        MapView map = (MapView) findViewById(R.id.map);
        map.setTileSource(TileSourceFactory.MAPNIK);
    }

    public void onResume(){
        super.onResume();
        //this will refresh the osmdroid configuration on resuming.
        //if you make changes to the configuration, use
        //SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        //Configuration.getInstance().save(this, prefs);
        Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
    }
}

Add default zoom buttons, and ability to zoom with 2 fingers (multi-touch)

map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);

Move the map to a default view point. For this, access the map controller:

IMapController mapController = map.getController();
        mapController.setZoom(9);
        GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
        mapController.setCenter(startPoint);

Source and sample project can be found at: GitHub