Skip to content

π™»πš˜πšŒπšŠπšπš’πš˜πš— πšƒπš›πšŠπšŒπš”πš’πš—πš 🧭

Devrath edited this page Dec 29, 2022 · 1 revision
**Contents**
Setup
About permissions used in the app for location tracking
Getting current users location
Locate the current user
Service to track the users moment in background

πš‚πšŽπš-πš„πš™ πŸ‘£

πŸ”° Steps for creating a debug key for map-API

* Get the package name from your manifest
* Get the SHA-1 certificate fingerprint from your application(You can use GradleTasks -> android -> siginingReport)
* There is a useful link to generate the SHA-1 key(https://stackoverflow.com/a/68728675/1083093)
* Place the SHA Keys: https://console.cloud.google.com/google/maps-apis/credentials?project=distance-tracker-369716
* Enable google maps SDK for Android and get the key, Useful link(https://developers.google.com/maps/documentation/android/start#get-key)
* Then add a string resource(SECRET.xml) in project location SRC -> DEBUG -> RES -> VALUES -> SECRET.XML and the key
* <resources><string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">dddsdjbeioudhqweuhd8</string></resources>

πŸ”° Steps for creating a release key for map-API

* Generate a keystore file by entering the proper credentials
* Use the keystore file to generate the SHA-1 key 
* COMMAND➜ keytool -list -v -keystore /Users/devrath/keys/Keystore -alias AliasName
* Useful link:➜ How to get API key: https://developers.google.com/maps/documentation/android/start#get-key 
* Place the SHA Keys: https://console.cloud.google.com/google/maps-apis/credentials?project=distance-tracker-369716
* Then add a string resource(SECRET.xml) in project location SRC -> RELEASE -> RES -> VALUES -> SECRET.XML and the key
* <resources><string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">SHJJS8s8shhdsdssdss</string></resources>

πŸ”° Create firebase account

* Since the project uses firebase to register crashes we need to create a firebase account.
* Place the google-services.json in the project app level.

π™°πš‹πš˜πšžπš πš™πšŽπš›πš–πš’πšœπšœπš’πš˜πš—πšœ 𝚞𝚜𝚎𝚍 πšπš˜πš› πšπš‘πšŽ πšŠπš™πš™ πšπš˜πš› πš•πš˜πšŒπšŠπšπš’πš˜πš— πšπš›πšŠπšŒπš”πš’πš—πš πŸ”‘

β–ͺ️ In android sharing user location is very delicate so a series of permissions need to be provided from user β–ͺ️ There are two scenarios to handle when it comes to runtime permissions ACCESS_FINE_LOCATION, ACCESS_BACKGROUND_LOCATION

<!--
DESCRIPTION: -> The permission ACCESS_FINE_LOCATION is needed to get the user's location from the device
RUNTIME-PERMISSION: -> Needed
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!--
DESCRIPTION: -> The permission ACCESS_BACKGROUND_LOCATION is needed from android-10 and above in older version its by default provided
RUNTIME-PERMISSION: -> Needed
-->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!--
DESCRIPTION: -> The permission FOREGROUND_SERVICE is needed to display the foreground notification as a service
RUNTIME-PERMISSION: -> Not needed
-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Runtime Permission Background tracking permission

π™ΆπšŽπšπšπš’πš—πš πšŒπšžπš›πš›πšŽπš—πš πšžπšœπšŽπš› πš•πš˜πšŒπšŠπšπš’πš˜πš— 🌍

  • We use fused-location-provider to get the current location of the user
  • In earlier API's it was very complex to get user's current location. With the fused location provider encapsulates all the logic and provides the current location from the user
  • In our app we request the last known position of the user in certain intervals of time and get the specific lat/lng for that location.
  • So, the entire journey we get a list of lat/lng and we draw a polyline for complete list creating a path

π™»πš˜πšŒπšŠπšπšŽ πšπš‘πšŽ πšŒπšžπš›πš›πšŽπš—πš πšžπšœπšŽπš› πŸŒ€

  • Once the map is loaded, We can access the Location button on the map and by simulating a click action on the map, we and redirect the camera to the user location.
Locating user

πš‚πšŽπš›πšŸπš’πšŒπšŽ 𝚝𝚘 πšπš›πšŠπšŒπš” πšžπšœπšŽπš›πšœ πš–πš˜πš–πšŽπš—πš πš’πš— πš‹πšŠπšŒπš”πšπš›πš˜πšžπš—πš β˜‚οΈ

  • When we require tracking the user journey we need to use the fused location service continously at certain intervals of time and get the lat/lng position of the user
  • This tracking needs to happen continously regards of application is in foreground or background. In this scenario we need to use a foreground service to run the location mechanish continously which also require a notification to be shown on the app tray which displays distance travelled.
  • When new location update is available, we need to add that location to the list of locations maintained as live data in the service.
  • We observe the live data from the fragment and update the map polyline and camera position accordingly.
Clone this wiki locally