Skip to main content

Add a map

Open the activity you would like to add a map view to and use the code below.

public class SimpleMapActivity extends AppCompatActivity {

private OsmandApplication app;
private OsmandMapTileView mapTileView;
private MapViewWithLayers mapViewWithLayers;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_map_activity);
mapViewWithLayers = findViewById(R.id.map_view_with_layers);

app = (OsmandApplication) getApplication();

mapTileView = app.getOsmandMap().getMapView();
mapTileView.setupOpenGLView();

//set start location and zoom for map
mapTileView.setIntZoom(14);
mapTileView.setLatLon(52.3704312, 4.8904288);
}
}

Open the activity’s XML layout file and add the following:

    <net.osmand.plus.views.MapViewWithLayers
android:id="@+id/map_view_with_layers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />

The MapViewWithLayers contains its own lifecycle methods. In order for your app to correctly call the MapView's lifecycle methods, you must override the following lifecycle methods in the Activity that contains the MapViewWithLayers and call these methods.


@Override
protected void onResume() {
super.onResume();
mapViewWithLayers.onResume();
}

@Override
protected void onPause() {
super.onPause();
mapViewWithLayers.onPause();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapViewWithLayers.onDestroy();
}