Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Current Latitude and Longitude without Internet connection in android?

I am creating an app related to location, so i need to get current latitude and longitude without having Internet Connection.

Please help me out. I am novice with Location Manager. Suggestion appreciated. Thanks Regards.

like image 227
Shoeb Ahmed Siddique Avatar asked Dec 12 '25 21:12

Shoeb Ahmed Siddique


1 Answers

There are Two ways to get the Location

1)Network provider

2)GPS Provider

for getting location through GPS Provider(without Internet) Use LocationManager.GPS_PROVIDER as mentioned in the below Code

locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {

            location =locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();

                            }
                        }

and to know about the GPS status, whether enabled or not

boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

and for Removing Updated

if(locationManager != null){locationManager.removeUpdates(this);
like image 145
Jamil Avatar answered Dec 14 '25 10:12

Jamil