Android Fetch Current Latitude and Longitude Values

 

Android Fetch Latitude Longitude :

In General few apps require user location, there we can enter address in a edittext field but as to get much more flexibility we can fetch the current latitude and longitude details based on user location. i.e., Android Fetch Latitude Longitude values from gps based on location of user we fetch these values.

 

Many apps now a days like taxi apps which will show their cabs present locations based on these values and also there are many different types of usages.

Food delivery apps, grocery delivery apps and lot more apps due to lockdown 2020 started using apps which mostly required user location.

Railway’s also make use of this location detection to track the train current status and inform the same to the passengers who travel.

So lets get started with Android Fetch Latitude Longitude Values based on user device.

 

Before getting further we will get values based on network provider also.

Now we need to check GPS is enabled or not to

boolean GPSEnable=false;
boolean NetWorkEnabled=false;

 

Now checking with Network

 

if(NetWorkEnabled)
{
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_FOR_UPDATES,MIN_DISTANCE_TO_REQUEST_LOCATION,this);
    if(locationManager!=null)
    {
        location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if(location!=null)
        {
            latitude=location.getLatitude();
            longitude=location.getLongitude();
        }
    }
}

 

Checking with GPS

 

if(GPSEnable)
{
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_FOR_UPDATES,MIN_DISTANCE_TO_REQUEST_LOCATION,this);
    if(locationManager!=null)
    {
        location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location!=null)
        {
            latitude=location.getLatitude();
            longitude=location.getLongitude();
        }
    }
}

 

FetchLocation.java

Providing the code for the class Android Fetch Latitude Longitude so that you can easily make use of it and fetch the exact location.

 

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;


public class FetchLocation extends Service implements LocationListener {

    private final Context con;

    boolean GPSEnable=false;
    boolean NetWorkEnabled=false;
    boolean LocationEnabled=false;
    private static final long MIN_DISTANCE_TO_REQUEST_LOCATION=1;
    private static final long MIN_TIME_FOR_UPDATES=1000*1;

    Location location;
    double latitude,longitude;
    LocationManager locationManager;

    public FetchLocation(Context context)
    {
        this.con=context;
        checkIfLocationAvailable();
    }

    public Location checkIfLocationAvailable()
    {
        try
        {
            locationManager=(LocationManager)con.getSystemService(LOCATION_SERVICE);

            GPSEnable=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            NetWorkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if(!GPSEnable && !NetWorkEnabled)
            {
                LocationEnabled=false;

                Toast.makeText(con,"Provider Not Enabled", Toast.LENGTH_SHORT).show();
            }
            else {
                LocationEnabled=true;

                if(NetWorkEnabled)
                {
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_FOR_UPDATES,MIN_DISTANCE_TO_REQUEST_LOCATION,this);
                    if(locationManager!=null)
                    {
                        location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if(location!=null)
                        {
                            latitude=location.getLatitude();
                            longitude=location.getLongitude();
                        }
                    }
                }
                if(GPSEnable)
                {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_FOR_UPDATES,MIN_DISTANCE_TO_REQUEST_LOCATION,this);
                    if(locationManager!=null)
                    {
                        location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if(location!=null)
                        {
                            latitude=location.getLatitude();
                            longitude=location.getLongitude();
                        }
                    }
                }
            }
        }catch (Exception e)
        {
        }
        return location;
    }


    public double getLatitude()
    {
        if(location!=null)
        {
            latitude=location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude()
    {
        if(location!=null)
        {
            longitude=location.getLongitude();
        }
        return longitude;
    }


    @Override
    public void onLocationChanged(Location location) {
    }
    @Override
    public void onProviderDisabled(String provider) {
    }
    @Override
    public void onProviderEnabled(String provider) {
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

 

 

MainActivity.java :

 

Now as a part of Android Fetch Latitude Longitude we will fetch the methods getLatitude and getLongitude and show output in MainActivity.java

 

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {

    TextView latitudetxt,longitudetxt;

    Button getlatlon;

    FetchLocation values;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        latitudetxt=(TextView)findViewById(R.id.latitude);
        longitudetxt=(TextView)findViewById(R.id.longitude);

        getlatlon=(Button)findViewById(R.id.getlocation);


        getlatlon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                values=new FetchLocation(MainActivity.this);


                 double latitude=values.getLatitude();
                 double longitude=values.getLongitude();

                latitudetxt.setText("Latitude= " + latitude );

                longitudetxt.setText("Longitude= " + longitude);

            }
        });
    }

    
}

 

And then

 

activity_main.xml :

 

Here we are adding two textviews and a button to fetch values on click.

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/getlocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/location"
        android:layout_marginTop="100dp"
        android:text="Current Latitude and Longitude Values" />

    <TextView
        android:id="@+id/latitude"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Latitude = 17.4010673"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_marginTop="80dp"/>

    <TextView
        android:id="@+id/longitude"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:text="Longitude = 78.5081861"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"/>



</LinearLayout>

 

AndroidManifest.xml :

 

Adding required permission for Android Fetch Latitude Longitude in manifest file like internet and access fine location.

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

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="geo.androidcoding.abhishek.getlatlon">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

Android Fetch Latitude Longitude Output :

The screen depicts Android Fetching Latitude and Longitude values.

Android Fetch Latitude Longitude

If you have any query in this tutorial on Android Fetch Latitude Longitude do let us know in comment section below. If you like the tutorial do share and like us for more android tutorials.

 

Show Buttons
Hide Buttons
Read previous post:
Android Marquee Animation: Unlock the Power of Animation

  Android Marquee Animation : In Android, when we aim to showcase text that captures the user's attention or display...

Close