Ask Your Question
3

How to integrate Open Street Map (OSM) into an Android app using Kotlin and an OSM URL that includes a Relation ID?

asked 2023-07-20 10:40:55 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-07-20 10:51:02 +0000

djk gravatar image
  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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-20 10:40:55 +0000

Seen: 19 times

Last updated: Jul 20 '23