How To Integrate OpenStreetMap Library In Android Application 2019
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)
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.BitmapFactoryimport android.os.Bundleimport android.preference.PreferenceManagerimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport com.app.base.BuildConfigimport com.app.base.base_classes.BaseFragmentimport com.google.android.gms.maps.GoogleMapimport org.osmdroid.api.IMapControllerimport org.osmdroid.config.Configurationimport org.osmdroid.tileprovider.tilesource.TileSourceFactoryimport org.osmdroid.views.MapViewimport org.osmdroid.views.overlay.mylocation.GpsMyLocationProviderimport org.osmdroid.views.overlay.mylocation.MyLocationNewOverlayclass 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!!.applicationContextConfiguration.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 MapViewmap.setTileSource(TileSourceFactory.MAPNIK)//map.setBuiltInZoomControls(true) //Map ZoomIn/ZoomOut Button Visibilitymap.setMultiTouchControls(true)val mapController: IMapControllermapController = 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