Android · News

Android: Current Location Using Fused APi on Google Maps

This tutorial gives us the simple implementation of “Fused API” to fetch the current location on google map in android. Fused API is latest among all techniques to get the location. It provides you very precise results and also uses less battery of your device.

It chooses GPS or Network provider to get to your current location. And it helps your device remember about the last saved location. Let’s implement the Fused API to fetch/get the current location.
Step 1: Create a new project in Android studio. and select Maps Activity.

Step 2: Give name to your project,set location and click finish.

Step 3: Now before moving forward get a API key from Google Console. Create a new project in Google console. and then click Enable API

Step 4: Click on Google Maps Android API, to enable this api. After that click on Credentials option given on the left side bar.
Then, Click “Create Credentials” to create your API key. Copy it and paste it in xml file, as shown in below image.

Step 5: Goto Values folder and  open “google_maps_api.xml” file. Inside this file, there is string which says “YOUR_KEY_HERE”. Replace this text with your API key.

Step 6: Add the below permission to Android Manifest file.

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

Step 7: Use this code given below to get the current Location on Google Map.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,  
LocationListener {

    private GoogleMap mMap;
    public static double latitude;
    public static double longitude;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;
    LocationManager locationManager;
    LocationRequest mLocationRequest;
    LocationListener mlocationListener;

    protected void onCreate(Bundle savedInstanceState) {

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()


    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng position = new LatLng(latitude, longitude );
        mMap.addMarker(new MarkerOptions().position(position).title("Your Current Location"));

 protected synchronized void buildGoogleApiClient()

        mGoogleApiClient = new GoogleApiClient.Builder(this)

    public void onConnected( Bundle bundle) {


        if (ContextCompat.checkSelfPermission(this, 
android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)

            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);



    public void onConnectionSuspended(int i) {


    public void onConnectionFailed( ConnectionResult connectionResult) {


    protected void onStart() {




    protected void onStop() {

       // Toast.makeText(this, "OnStop", Toast.LENGTH_SHORT).show();

    public void onLocationChanged(Location location) {

        mLastLocation = location;
        latitude= location.getLatitude();
        longitude = location.getLongitude();

     //  Toast.makeText(this, "Lat: "+latitude+", Lng: "+longitude, Toast.LENGTH_SHORT).show();
        //stop location updates
        if (mGoogleApiClient != null)
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

XML File:
<fragment xmlns:android=""
    tools:context="com.currentlocationusingfusedapi.MapsActivity" />

One thought on “Android: Current Location Using Fused APi on Google Maps

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s