Locate Android device location on GoogleMap

Tram Ho

Today most applications are large, integrating GoolgeMap service and their application location to identify the user’s location or the user’s device. Typically, Grab, Uber, now … use GoogleMap and location to make it easy for users to move and locate customers. In this article, I will introduce how to integrate GoogleMap and device positioning. android inform on GoogleMap

GoogleMap integration

Configure and set up Manifest file

Open your build.gradle file and add this dependency to the android project

Open Manifiest file to add some permissions

Add the following declaration to your manifying file inside the root element of the manifiest file.

Add the following declaration to your manifying file, You need to put it inside the Application tab

Add API key in project manifest and declare key API in values ​​/ string

Add the GoogleMap in MainActivity.XML object

Setting in MainActivity.java

Run the application and see the results

Locate Android on GoogleMap

Let’s start writing code

Add some permissions to locate

In MainActivity.java you implement the following objects: onnectionCallbacks, OnConnectionFailedListener and LocationListener

Write a number of methods to locate

  1. setupLocationManager () – Use to create LocationManager before requesting endpoint location
  2. createdLocationRequest () – Processing required parameters to receive the endpoint location
  3. setInitialLocation () – This method displays the device’s last known Location on GoogleMap

Write a code for the setupLocationManager () method.

Code for the createdLocationRequest method ()

You need to process the result via OnActivityResult

Code the setInitialLocation () method: this method is responsible for room on-screen location to your location.

Handling location

Check the application version and ask the user to grant permission for the application version 6.0

Finally call the methods back to onCreate ()

Run the application and see the results:

Share the news now

Source : Viblo