Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  1. Add the OSMdroid library to your project by adding the following line to your app's build.gradle file:
implementation 'org.osmdroid:osmdroid-android:6.1.10'
  1. Create a MapView in your layout file:
<org.osmdroid.views.MapView
   android:id="@+id/map"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tilesource="Mapnik"
   />
  1. Initialize the MapView in your activity's onCreate method:
override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   setContentView(R.layout.activity_main)

   val map = findViewById<MapView>(R.id.map)
   map.setTileSource(TileSourceFactory.MAPNIK)
   map.setMultiTouchControls(true)
}
  1. Load the OSM data by creating a new instance of the OsmParser and passing in the OSM URL that includes a Relation ID:
val osmUrl = "https://www.openstreetmap.org/relation/{relationID}"
val relationId = 12345 // Replace with actual relation ID
val osmParser = OsmParser(osmUrl.replace("{relationID}", relationId.toString()), applicationContext)
val mapView = findViewById<MapView>(R.id.map)
osmParser.load(mapView)
  1. When the OSM data has finished loading, you can add markers or other map features using the MapView's Overlays:
osmParser.onLoadComplete = {
   // Add markers, lines, or other map features using the MapView's Overlays
   val startPoint = GeoPoint(40.748817, -73.985428) // Example location in New York City
   val marker = Marker(mapView)
   marker.position = startPoint
   mapView.overlays.add(marker)
   mapView.invalidate()
}

That's it! Your app should now display the OSM data for the specified Relation ID.