Dark Mode
Image

Android Fragments

Android Service

Android AlarmManager

Camera Tutorial

Sensor Tutorial

Android Graphics

Android Animation

Android Web Service

Android MCQ

Android Quiz

Google Map Displaying Current Location

In the previous tutorial of Android Google Map, we simply displayed the default coordinates (location) set by the MapsActivity.java class file.

Now in this tutorial we will display and place marker at the user current location. For doing this we need to generate Google Map API key. The process of generating Google Map API is described in tutorial Android Google Map.

To display the user current location we need to implements some interfaces and there callbacks methods.

Callback methods in Google Map

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

The setMyLocationEnabled() method of GoogleMap is used to enable location layer, which allows device to interact with current location.

Example of Google Map Displaying Current Location

Let's see an example of Google Map which displays the current location of device.

activity_maps.xml

Add a SupportMapFragment in fragment in activity_maps.xml file.

<fragment xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:map="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/map"  
    android:name="com.google.android.gms.maps.SupportMapFragment"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.com.mapexample.MapsActivity" />  

build.gradel

Add the following dependencies in build.gradel file.

dependencies {  
    implementation fileTree(dir: 'libs', include: ['*.jar'])  
    implementation 'com.android.support:appcompat-v7:26.1.0'  
    implementation 'com.google.android.gms:play-services-maps:11.8.0'  
    compile 'com.google.android.gms:play-services-location:11.8.0'  
    testImplementation 'junit:junit:4.12'  
    androidTestImplementation 'com.android.support.test:runner:1.0.1'  
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'  
  
}  

MapsActivity.java

Add the following code in MapsActivity.java file.

package example.com.mapexample;  
  
  
import android.os.Build;  
import android.support.v4.app.FragmentActivity;  
import android.os.Bundle;  
  
import com.google.android.gms.common.api.GoogleApiClient;  
import com.google.android.gms.maps.CameraUpdateFactory;  
import com.google.android.gms.maps.GoogleMap;  
import com.google.android.gms.maps.OnMapReadyCallback;  
import com.google.android.gms.maps.SupportMapFragment;  
import com.google.android.gms.maps.model.BitmapDescriptorFactory;  
import com.google.android.gms.maps.model.LatLng;  
import com.google.android.gms.maps.model.Marker;  
import com.google.android.gms.maps.model.MarkerOptions;  
import com.google.android.gms.location.LocationServices;  
  
import android.location.Location;  
import android.Manifest;  
import android.content.pm.PackageManager;  
import android.support.v4.content.ContextCompat;  
import com.google.android.gms.common.ConnectionResult;  
import com.google.android.gms.location.LocationListener;  
import com.google.android.gms.location.LocationRequest;  
  
  
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,  
        LocationListener,GoogleApiClient.ConnectionCallbacks,  
        GoogleApiClient.OnConnectionFailedListener{  
  
    private GoogleMap mMap;  
    Location mLastLocation;  
    Marker mCurrLocationMarker;  
    GoogleApiClient mGoogleApiClient;  
    LocationRequest mLocationRequest;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_maps);  
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.  
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()  
                .findFragmentById(R.id.map);  
        mapFragment.getMapAsync(this);  
  
    }  
  
    @Override  
    public void onMapReady(GoogleMap googleMap) {  
        mMap = googleMap;  
  
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
            if (ContextCompat.checkSelfPermission(this,  
                    Manifest.permission.ACCESS_FINE_LOCATION)  
                    == PackageManager.PERMISSION_GRANTED) {  
                buildGoogleApiClient();  
                mMap.setMyLocationEnabled(true);  
            }  
        }  
        else {  
            buildGoogleApiClient();  
            mMap.setMyLocationEnabled(true);  
        }  
  
    }  
    protected synchronized void buildGoogleApiClient() {  
        mGoogleApiClient = new GoogleApiClient.Builder(this)  
                .addConnectionCallbacks(this)  
                .addOnConnectionFailedListener(this)  
                .addApi(LocationServices.API).build();  
        mGoogleApiClient.connect();  
    }  
  
    @Override  
    public void onConnected(Bundle bundle) {  
  
        mLocationRequest = new LocationRequest();  
        mLocationRequest.setInterval(1000);  
        mLocationRequest.setFastestInterval(1000);  
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);  
        if (ContextCompat.checkSelfPermission(this,  
                Manifest.permission.ACCESS_FINE_LOCATION)  
                == PackageManager.PERMISSION_GRANTED) {  
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);  
        }  
  
    }  
  
    @Override  
    public void onConnectionSuspended(int i) {  
  
    }  
  
    @Override  
    public void onLocationChanged(Location location) {  
  
        mLastLocation = location;  
        if (mCurrLocationMarker != null) {  
            mCurrLocationMarker.remove();  
        }  
        //Place current location marker  
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());  
        MarkerOptions markerOptions = new MarkerOptions();  
        markerOptions.position(latLng);  
        markerOptions.title("Current Position");  
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));  
        mCurrLocationMarker = mMap.addMarker(markerOptions);  
  
        //move map camera  
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));  
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));  
  
        //stop location updates  
        if (mGoogleApiClient != null) {  
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);  
        }  
  
    }  
  
    @Override  
    public void onConnectionFailed(ConnectionResult connectionResult) {  
  
    }  
  
}  

Request Runtime Permission

Android device having Android 6.0 (Marshmallow) or later are required some permission at runtime to access device functionality.

In the above MapsActivity.java file we added a runtime permission Manifest.permission.ACCESS_FINE_LOCATION which request to access device location. The runtime permission is checked using checkSelfPermission() method and return PackageManager.PERMISSION_GRANTED or PackageManager.PERMISSION_DENIED. If permission granted than app proceeds for operation.

Required Permission in AndroidManifest.xml

Add the following user-permission in AndroidManifest.xml file.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
<uses-permission android:name="android.permission.INTERNET" />  

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="example.com.mapexample">  
    <!--  
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use  
         Google Maps Android API v2, but you must specify either coarse or fine  
         location permissions for the 'MyLocation' functionality.   
    -->  
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
    <uses-permission android:name="android.permission.INTERNET" />  
  
  
    <application  
        android:allowBackup="true"  
        android:icon="@mipmap/ic_launcher"  
        android:label="@string/app_name"  
        android:roundIcon="@mipmap/ic_launcher_round"  
        android:supportsRtl="true"  
        android:theme="@style/AppTheme">  
        <!--  
             The API key for Google Maps-based APIs is defined as a string resource.  
             (See the file "res/values/google_maps_api.xml").  
             Note that the API key is linked to the encryption key used to sign the APK.  
             You need a different API key for each encryption key, including the release key that is used to  
             sign the APK for publishing.  
             You can define the keys for the debug and release targets in src/debug/ and src/release/.   
        -->  
        <meta-data  
            android:name="com.google.android.geo.API_KEY"  
            android:value="@string/google_maps_key" />  
  
        <activity  
            android:name=".MapsActivity"  
            android:label="@string/title_activity_maps">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
  
</manifest>  

Output

android Current Location

Comment / Reply From