In this blog we will learn to use open street map (OSM), adding marker and get device location.
OpenStreetMap (OSM) is absolutely editable Wikipedia like world map which is also free. Through openstreetmap you can easily download maps for the offline purpose, this is the only big advantage of OSM over Google Maps, which require internet for map downloading. With openstreetmap, a map can be flawlessly downloaded to an application and can be utilized without internet.
![](http://findnerd.s3.amazonaws.com/imagedata/9862/9862.png)
Below are some steps to follow to integrate OSM in your android application .
1) Add library path in your application gradle file :-
compile 'org.osmdroid:osmdroid-android:6.0.0'
2) Add map in you activity XML,For example :-
<org.osmdroid.views.MapView
android:id="@+id/map"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/mapOptions"
app:layout_constraintLeft_toRightOf="@id/mapGrid"
/>
3) In you activity java file,Add below code to show map:-
binding.map.setTileSource(TileSourceFactory.MAPNIK);
binding.map.setBuiltInZoomControls(false); //Map ZoomIn/ZoomOut Button Visibility
binding.map.setMultiTouchControls(true);
IMapController mapController;
mapController = binding.map.getController();
mapController.zoomTo(zoomLevel,zoomSpeed);
4) If you need to show device location and zoom to device location then add below code in your activity:-
GpsMyLocationProvider mGpsMyLocationProvider = new GpsMyLocationProvider(this);
MyLocationNewOverlay mLocationOverlay = new MyLocationNewOverlay(mGpsMyLocationProvider,binding.map);
mLocationOverlay.enableMyLocation();
mLocationOverlay.enableFollowLocation();
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.person);
mLocationOverlay.setPersonIcon(icon);
binding.map.getOverlays().add(mLocationOverlay);
mLocationOverlay.runOnFirstFix(new Runnable() {
@Override
public void run() {
binding.map.getOverlays().clear();
binding.map.getOverlays().add(mLocationOverlay);
mapController.animateTo(mLocationOverlay.getMyLocation());
}
});
Thanks for Reading
Happy Coding !!!
0 Comment(s)