Skip to content

Instantly share code, notes, and snippets.

@mwshubham
Created July 10, 2018 19:14
Show Gist options
  • Save mwshubham/f2c1e563cc76fd58ca904651dbea65a8 to your computer and use it in GitHub Desktop.
Save mwshubham/f2c1e563cc76fd58ca904651dbea65a8 to your computer and use it in GitHub Desktop.
Demo OSM map initialization
class TransportActivity : AppCompatActivity() {
companion object {
val TAG = TransportActivity::class.simpleName
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//handle permissions first, before map is created. not depicted here
//load/initialize the osmdroid configuration, this can be done
Configuration.getInstance().load(applicationContext, PreferenceManager.getDefaultSharedPreferences(applicationContext))
//setting this before the layout is inflated is a good idea
//it 'should' ensure that the map has a writable location for the map cache, even without permissions
//if no tiles are displayed, you can try overriding the cache path using Configuration.getInstance().setCachePath
//see also StorageUtils
//note, the load method also sets the HTTP User Agent to your application's package name, abusing osm's tile servers will get you banned based on this string
//inflate and create the map
setContentView(R.layout.activity_transport)
setSupportActionBar(toolbar)
init()
}
override fun init() {
map_view.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE)
map_view.setBuiltInZoomControls(true)
map_view.setMultiTouchControls(true)
map_view.controller.setZoom(15.0)
val gPt = GeoPoint(28.6337874, 77.35767599999997)
val gPt2 = GeoPoint(28.638472, 77.360667)
map_view.controller.setCenter(gPt)
/*place icon on map*/
val items = ArrayList<OverlayItem>()
items.add(OverlayItem("Current Location", "Amrapali Village, Indirapuram", gPt))
items.add(OverlayItem("Destination", "Aditya Mall, Indirapuram", gPt2))
val mOverlay = ItemizedOverlayWithFocus<OverlayItem>(items,
object : ItemizedIconOverlay.OnItemGestureListener<OverlayItem> {
override fun onItemSingleTapUp(index: Int, item: OverlayItem): Boolean {
//do something
Log.d(TAG, "onItemSingleTapUp()")
return true
}
override fun onItemLongPress(index: Int, item: OverlayItem): Boolean {
Log.d(TAG, "onItemLongPress()")
return false
}
}, this)
mOverlay.setFocusItemsOnTap(true)
map_view.overlays.add(mOverlay)
}
public override fun 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().load(this, PreferenceManager.getDefaultSharedPreferences(this));
map_view.onResume() //needed for compass, my location overlays, v6.0.0 and up
}
public override fun onPause() {
super.onPause()
//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);
map_view.onPause() //needed for compass, my location overlays, v6.0.0 and up
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment