Thursday, 9 April 2020

Integrate OpenStreetMap in android application

  4 comments
In this post, i am going to show you a simplest way to integrate the OpenStreeMap in android application. So without going into deep explanation let's create sample app that show a user current location on the map.

Step 1 : 
Add the osm library in your build.gradle(app level)

implementation 'org.osmdroid:osmdroid-android:6.0.0'


Step 2 :
Add permissions in the AndroidManifest.xml file

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


Step 3 :
Create a fragment or activity according to your wish and in my case i have created a fragment. Do the copy and paste of below code into your project

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
tools:context="com.app.base.ui.location.LocationFragment">
<org.osmdroid.views.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
import android.graphics.BitmapFactory
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.app.base.BuildConfig
import com.app.base.base_classes.BaseFragment
import com.google.android.gms.maps.GoogleMap
import org.osmdroid.api.IMapController
import org.osmdroid.config.Configuration
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
class LocationFragment : BaseFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(com.app.base.R.layout.fragment_location, container, false)
val ctx = activity!!.applicationContext
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
Configuration.getInstance().setUserAgentValue(BuildConfig.APPLICATION_ID);
val map = view.findViewById<MapView>(com.app.base.R.id.mapview)
map.setUseDataConnection(true)
//val map = view.findViewById(R.id.map) as MapView
map.setTileSource(TileSourceFactory.MAPNIK)
//map.setBuiltInZoomControls(true) //Map ZoomIn/ZoomOut Button Visibility
map.setMultiTouchControls(true)
val mapController: IMapController
mapController = map.getController()
//mapController.zoomTo(14, 1)
mapController.setZoom(14)
val mGpsMyLocationProvider = GpsMyLocationProvider(activity)
val mLocationOverlay = MyLocationNewOverlay(mGpsMyLocationProvider, map)
mLocationOverlay.enableMyLocation()
mLocationOverlay.enableFollowLocation()
val icon = BitmapFactory.decodeResource(resources, com.app.base.R.drawable.ic_menu_compass)
mLocationOverlay.setPersonIcon(icon)
map.getOverlays().add(mLocationOverlay)
mLocationOverlay.runOnFirstFix {
map.getOverlays().clear()
map.getOverlays().add(mLocationOverlay)
mapController.animateTo(mLocationOverlay.myLocation)
}
return view
}
}

Image

4 comments :

  1. thank.can we have the java code?

    ReplyDelete
  2. i implemented but it don't work what's the problem i can't see the map

    ReplyDelete