OSM wants these user permissions, we have to add these on our Manifest
Layout
Create a “src/main/res/layouts/main.xml” layout like this one. With Android Studio, it probably created one already called. The default is “src/main/res/layouts/activity_main.xml”:
Note: It does not have to be LinearLayout, you can use whatever layout you want
Main Activity
After we configure OSM, we need to initialize our OSM map in @onCreate method:
public class MainActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MapView mMapView = (MapView) findViewById(R.id.map);
// OSM Map Initialize
//important! set your user agent to prevent getting banned from the osm servers
org.osmdroid.tileprovider.constants.OpenStreetMapTileProviderConstants.setUserAgentValue(BuildConfig.APPLICATION_ID);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
// Setup MapView
setupMapView();
}
}
Setup OSM Method
After we’ve done onCreate method, we need to determine our map’s properties such as; zoom buttons, multi touch control, default zoom and default point.
As you can see the last two (2) lines of code setting the default map’s coordinates.
// Setup the mapView controller:
public void setupMapView(){
// Zoom buttons
mMapView.setBuiltInZoomControls(true);
// Multitouch Controls Activation
mMapView.setMultiTouchControls(true);
mMapView.setClickable(true);
// Default map zoom level:
//private int MAP_DEFAULT_ZOOM = 20;
mMapView.getController().setZoom(MAP_DEFAULT_ZOOM);
// Default Point
GeoPoint startPoint = new GeoPoint(37779300, -122419200);
mMapView.getController().setCenter(startPoint);
}