0

I want to collect the Latitude and Longitude from user device.

There are multiple ways to do so. Right now am collecting the lat/lang from Google's API <--- SO Answer.

Now there are also multiple methods in which they check the provider i.e either GPS OR Network Provider and collect the location using LocationListener and collect the data.

A sample code will be this <--- Again a SO answer.

Both of them are working but my question is , what if the user has given the Location Permission and then turned of the Location service from settings. In that case I'll like to collect the lat/lang solely from the network provider.

But when I try the above codes after turning off the Location

enter image description here I get null as the location.

Every time the requestLocationUpdates returns a null location.

Basically I want a fall through options to collect the lat/long.

1-> Google Api (When location is on) 2-> LocationMangaer (When location is on) 3-> Get lat/lang from cellular tower (When location is off, wifi is off and GPS is off)

Community
  • 1
  • 1
driftking9987
  • 1,673
  • 1
  • 32
  • 63
  • You can't get the network latlong unless GPS setting is turned on to `Battery Saving Mode` or else you can use `opencellid` API's – Madhukar Hebbar Sep 15 '16 at 12:51
  • Could you please explain a bit about `opencellid` ? – driftking9987 Sep 15 '16 at 13:16
  • Here is the [API details](http://wiki.opencellid.org/wiki/API), [FAQ](http://wiki.opencellid.org/wiki/FAQ) and [API Keys](http://opencellid.org/#action=database.requestForApiKey) – Madhukar Hebbar Sep 15 '16 at 13:21
  • Thanks @MadhukarHebbar, but this is not what am looking for. It won't solve the purpose. – driftking9987 Sep 15 '16 at 13:30
  • _"...my question is , what if the user has given the Location Permission and then turned of the Location service from settings."_ Then you can not determine the device's location. That's the whole idea of turning the Location service off. (Or maybe you can find the cell tower id and follow Madhukar's idea for getting a rough location. I'm not familiar with that myself.) – Markus Kauppinen Sep 15 '16 at 14:00

1 Answers1

0
private boolean gps_enabled = false;
private boolean network_enabled = false;



locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

if (network_enabled) {
            getLocationDataByMobileNetwork();
        } else if (gps_enabled) {
            getLocationDataByGPS();
        }

private void getLocationDataByMobileNetwork() {
    try {
        appLocationService = new AppLocationService(getApplicationContext());
        Location nwLocation = appLocationService.getLocation(LocationManager.NETWORK_PROVIDER);
        if (nwLocation != null) {
            latitude = nwLocation.getLatitude();
            longitude = nwLocation.getLongitude();


        } else {
            Toast.makeText(getApplicationContext(),"(Network):not fetched your location",Toast.LENGTH_LONG).show();
        }
    } catch (Exception ex) { 
        Toast.makeText(getApplicationContext()," (Network) in Exception:not fetched your location",Toast.LENGTH_LONG).show();

    }
}



private void getLocationDataByGPS() {
    try {
        Location gpsLocation = appLocationService.getLocation(LocationManager.GPS_PROVIDER);
        if (gpsLocation != null) {
            latitude = gpsLocation.getLatitude();
            longitude = gpsLocation.getLongitude(); 

        } else {

        }
    } catch (Exception ex) {

    }

}
Rudresh
  • 731
  • 1
  • 10
  • 26