Get Current Location Google Maps Api Android

MainActivity.java


package com.example.pc_1.location;

import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {


    private  final String Log_Tag = "TestApp";
    private TextView txtOutput;
    private  GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        txtOutput = (TextView) findViewById(R.id.txtOutput);


    }


    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(1000);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);



    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    @Override
    public void onLocationChanged(Location location) {
        txtOutput.setText(location.toString());
        double lat =(double) location.getLatitude();
        double lan = (double) location.getLongitude();
        txtOutput.setText("Lat:"+lat+" lon:"+lan);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
     mGoogleApiClient.disconnect();

    }

}




Activity_main.xml:

This layout file contain a RelativeLayout and a EditText in it to show the device current latitude and longitude.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context="com.example.pc_1.location.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="location"
        android:id="@+id/txtOutput"
        android:textSize="24dp"
        android:textColor="@color/colorPrimaryDark"/>


</RelativeLayout>

For location service you need to add this dependency in build.gradle file or you can add latest one

compile 'com.google.android.gms:play-services:6.5.+'

also you need to add internet permission in Androidmanifest.xml file

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

Open your device location and run the app.

                                 Output:

 

 

You might also like More from author

Leave A Reply

Your email address will not be published.