Integrating Navigation Application using Huawei Site Kit, Map Kit, Location Kit and Direction API
This application helps us for getting the direction from current location to the selected place. This app uses Huawei Site Kit, Location Kit, Map kit and Huawei Direction API for showing the direction. Let us see the uses of all kits in this application.
- Site Kit: This kit is used for getting the places and near-by places on keyword search.
- Location Kit: This kit is used for getting the current location of the user.
- Map Kit: This kit is used for showing the map, adding a marker and drawing polyline on the Huawei Map.
- Direction API: This API is used for getting the path, steps and polyline between two places.
Let us start with the project configuration part:
Step 1: Create an app on App Gallery Connect.
Step 2: Enable the Site Kit, Location Lit and Map Kit in Manage APIs menu.
Step 3: Create an Android Project with the same package name as App Gallery project package name.
Step 4: Enter the below maven url inside the repositories of buildscript and allprojects (project build.gradle file).
Step 5: Add classpath to project’s build.gradle file.
Step 6: Apply plugin in App’s build.gradle file at top after application plugin.
Step 7: Add below dependencies to app’s build.gradle file.
Step 8: Add the app ID generated when the creating the app on HUAWEI Developers to manifest file.
Step 9: Add the below permissions to manifest file.
Step 10: Generate SHA 256 key and add to App Gallery Connect Project.
Step 11: download the agconnect-services.json from App Information Section. Copy and paste the Json file in the app folder of the android project.
Step 12: Sync the project.
Let us start with the implementation part:
Part 1: Site Kit Integration
Using the Site Kit, we will search for place and get the latitude and longitude.
Step 1: Get the API_KEY from App Gallery and define the same in your MainActivity.Java.
Step 2: Declare a SearchService object and use SearchServiceFactory to initialize the object.
Step 3: create the layout for search a place.
Step 4: Create the ListAdapter for showing the data.
Step 5: Create row_layout.xml inside layout folder.
Step 6: Initialize the Recycler view to MainActivity.Java.
Step 7: On Search button click, search places and set it to ListAdapter.
Now getting the list of places completed.
Part 2: Map Kit Implementation
This Kit is being used for showing the Huawei map. After clicking on Get Direction button in searched places, its navigates to DirectionActivity.Java which loads the Huawei map using Map Kit and getting the current location using Huawei Location Kit.
Step 1: Create the xml layout which contains the MapView.
Step 2: To use the MapView, implement OnMapReadyCallbackAPI and override the onMapReady(HuaweiMap huaweiMap).
Step 3: Add runtime permissions.
Step 4: Load MapView inside onCreate() method of DirectionActivity.Java and call getMapAsync() to register the callback.
Step 5: Inside onMapReady() callback, get the Huawei Map object and set the current location enabled.
Step 6: Override the onStart(), onStop(),onDestroy(),onPause(), onResume() and onLowMemory() in the DirectionActivity.Java.
Now integration of map done.
Part 3: Location Kit Integration
This kit is being used for getting the current location of the user.
Step 1: Initialize the current location instances.
Step 2: call getCurrentLocation() inside onCreate() method of DirectionActivity.Java and save it.
Step 3: call requestLocationUpdatesWithCallback() method after getCurrentLocation()inside onCreate().
Now getting current location part completed.
Part 4: Direction API Implementation
This API is getting used for getting the routes between source and destination location.
Step 1: Get the destination location from place info and save it inside onCreate()method of DirectionActivity.java.
Step 2: Create DirectionService.java for getting the routes between source and destination location.
Step 3: Call the getRoute() method inside getCurrentLocation() of DirectionActivity.Java.
Step 4: Create an Interface RouteInfo.
Step 5: DirectionActivity.Java will implement RouteInfo interface.
Step 6: Override routeInfo() method and convert string response to Json object using gson library.
Step 7: Add polyline from the routes info.
Step 8: Add a Marker at destination location.
Step 9: Add marker.xml to drawable folder.
Step 10: Animate camera to current location.
Now showing the direction between two place done.
Tips and Tricks:
Set input properly for origin and Destination for getting the routes.
This application can help to show the direction between your current location to your favorite place. You can get the restaurants, schools and places and navigate to the same.