Monday, 2 September 2019

How To Integrate OpenStreetMap Library In Android Application 2019

  No comments
Hello everyone, In this post i will show you a way to integrate OSM library that called as open stree map in android application and main advantage of using this library is that you don't need to pay anything to them as it is free of cost.
So do follow the below steps to implement this libary

Step 1: Add OSM library in build.gradle(project level)

implementation 'org.osmdroid:osmdroid-android:6.0.0'
Step 2: Create a LocationFragment then copy the below code.
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
    }
}
Step 3: Paste the below code into the xml file of LocationFragment class.
<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>



No comments :

Post a Comment

Loading...