Getting location offline
Asked Answered
I

1

8

I want to get the value of Longitude and Latitude of my current locatoin when offline and save the current location to its database. Is it possible to get the longitude and latitude of the device when mobile data and wifi are off but the GPS is ON?

Ingest answered 24/10, 2017 at 8:48 Comment(4)
Yes you can. The normal location listener class itself will work. but you can't update those values to your servers or DB without internet.Bondage
Hi @RakeshPolo, thanks for your comment, i just want to get the current/real-time location and save it on databaseIngest
You can save it on your physical device's DataBase.Bondage
yes, but i mean is it real time? like after a minute my location has change, the Latitude and Longitude will change without opening mobile connection or wifi but GPS is onIngest
B
16

Just use this code. Make sure the user hasn't selected wifi only or network only option in his location setings. it has to be high accuracy or GPS only. This piece of code will work.

public class Location extends AppCompatActivity {
LocationManager locationManager;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location);
    mContext=this;
    locationManager=(LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,
            2000,
            10, locationListenerGPS);
    isLocationEnabled();

}

LocationListener locationListenerGPS=new LocationListener() {
    @Override
    public void onLocationChanged(android.location.Location location) {
        double latitude=location.getLatitude();
        double longitude=location.getLongitude();
        String msg="New Latitude: "+latitude + "New Longitude: "+longitude;
        Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};


protected void onResume(){
    super.onResume();
    isLocationEnabled();
}

private void isLocationEnabled() {

    if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Enable Location");
        alertDialog.setMessage("Your locations setting is not enabled. Please enabled it in settings menu.");
        alertDialog.setPositiveButton("Location Settings", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                Intent intent=new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        AlertDialog alert=alertDialog.create();
        alert.show();
    }
    else{
        AlertDialog.Builder alertDialog=new AlertDialog.Builder(mContext);
        alertDialog.setTitle("Confirm Location");
        alertDialog.setMessage("Your Location is enabled, please enjoy");
        alertDialog.setNegativeButton("Back to interface",new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int which){
                dialog.cancel();
            }
        });
        AlertDialog alert=alertDialog.create();
        alert.show();
    }
}
}

The parameters of requestLocationUpdates methods are as follows:

provider: the name of the provider with which we would like to register. minTime: minimum time interval between location updates (in milliseconds). minDistance: minimum distance between location updates (in meters). listener: a LocationListener whose onLocationChanged(Location) method will be called for each location update.

Permissions:

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

Add above permissions to manifest file for the version lower than lollipop and for marshmallow and higher version use runtime permission.

Bondage answered 24/10, 2017 at 8:59 Comment(4)
thank-you so much... this code saved my day ! It was so simple, no need to get any service running or anything... !!!Tweeze
ive had to revert to using this because i need to get the location without a network. even though google docs says use fusedLocationClient.Paediatrics
Can i get location if i dont have Sim card on the device and even no internet?Johnettajohnette
anyone help me to do same in kotlin.Silvio

© 2022 - 2024 — McMap. All rights reserved.