Tracking App with Map and Location Kit
In this article we’re gonna talk about Map and Location features that Huawei has for developers via the app called wTracking.
wTracking allows you to track location data and draw map with it in real time. As you can see in the picture while you’re having the sport activity you choose map gets interacted with the user’s real-time data and draws on map even when you kill the app.
First we need to make sure to make configuration settings right. For Location and Map Kit configuration settings please check our documentary here. Than let’s get started with our application.
In the Android.Manifest file we add our permissions to be able to get the user’s location data.
In our MainActivity class we need to check for the permissions.
In our TrackingService class we need these variables.
We need to set our HMS objects in our locationStart method in the Service. We’re gonna use the callback when we need to listen the location asynchronously. We set MutableLiveData type locationListForPolyline variable with postValue method to be able to observe in MapActivity class.
In MapViewModel class we use addMarker() method to put a picture of a marker icon according to our user’s location data. With setPolyline() method we draw a line between the start and the end coordination info. And with zoomTarget we focus the camera in mapView according to user’s current coordination with 20f zoom level.
Here is the lifeCycle of our view and the HMS object that we called as map earlier. So we combine those lifeCycles here in our MapActivity class.
In MapActivity class we need those variables.
In MapActivity class’s onCreate method we set our context from the service. Create a bundle with the bundle key with the MAP_API_KEY that’s been created in our agconnect.json file earlier. After we set our view with the bundle we now have the access for getMapAsync callback that Huawei provides us. Than we set our map object with the instant data of the view.
If you want to do listening operations in synchronous way you can add OnMapReadyCallback interface to our Activity class and add the inherited member which onMapReady method than do the operations. But in our case we need to use async callbacks for that. Because we don’t know when exactly the location data comes.
In MapActivity class we have listenerLocation() method for start listening the location with observe method. And we set addMarker() and zoomTarget() if it’s the first time tracking. If not continue listening with trackLocation() method.
Here are some official documents if you want to check out for more detailed infos.
https://www.youtube.com/watch?v=BQbFlP4gRN8
There’s an article about how Foreground Service works via wTracking app. If
you want to check it out please make sure to visit here.
Hope my article helps you. See you in the next one soon. :)