Javatpoint Logo
Javatpoint Logo

Kotlin Android Google Map Current Location

To implement the Google Map in an Android application, we need to generate the Google Map API key. The process of creating the Google Map API key is described in Google Map Fixed Location tutorial.

In the previous tutorial of Kotlin Android Google Map Fixed Location, we have displayed the fixed Google location.

Now in this tutorial, we will display and place the marker at user's current location. To view the user's current location, we need to implement some Google Map interfaces and call there callbacks methods.

Google Map API Interface

  1. OnMapRreadyCallback: This interface invokes when its instance is set on a MapFragment object. The onMapReady(GoogleMap) method of OnMapReadyCallback interface is called when the map is ready to use. In the onMapReady(GoogleMap) method, we can add markers, listeners, and other attributes.
  2. LocationListener: This interface is used to receive the notification when the device location is changed. The abstract method onLocationChanged(Location) of LocationListener interface is called when the location has changed.
  3. GoogleApiClient.ConnectionCallbacks: This interface provides the callbacks methods onConnected(Bundle) and onConnectionSuspended(int) which are called when the device is connected and disconnected respectively.
  4. GoogleApiClient.OnConnectionFailedListener: This interface provides callbacks method onConnectionFailed(ConnectionResult) which is called when the error occurs while connecting the device to the service.

The isLocationEnabled property of GoogleMap is used to enable location layer, which allows the device to interact with the current location.

The tutorial using Java to display Google Map user's current location is described at Android Google Map Displaying Current Location.

Types of Google Map

There are four different types of Google map are available in map API. Each of them has different view of the map. These types are Normal, Hybrid, Satellite and Terrain.

  • googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  • googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  • googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
  • googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

Using Kotlin syntax, we will use the above Google map types as:

  • googleMap.mapType = MAP_TYPE_NONE
  • googleMap.mapType = MAP_TYPE_HYBRID
  • googleMap.mapType = MAP_TYPE_SATELLITE
  • googleMap.mapType = MAP_TYPE_TERRAIN

The detail about these map types are explain here.

activity_maps.xml

Add the following code in activity_maps.xml layout file.

strings.xml

build.gradle

Add the Google Map Service and Google Location Service dependencies in build.gradle file.

google_maps_api.xml

Place the Google Map API key in res/values/google_map_api.xml file.

MapsActivity.kt

Add the following code in MapsActivity.kt class file.

AndroidManifest.xml

Output:

Note: Check it on the real Android device for the best result of output.

Kotlin Android Google Map Current Location
Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA