Android Location Manager, Get Gps Location ,If No Gps Then Get to Network Provider Location

Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location

You're saying that you need GPS location first if its available, but what you did is first you're getting location from network provider and then from GPS. This will get location from Network and GPS as well if both are available. What you can do is, write these cases in if..else if block. Similar to-

if( !isGPSEnabled && !isNetworkEnabled) {

// Can't get location by any way

} else {

if(isGPSEnabled) {

// get location from GPS

} else if(isNetworkEnabled) {

// get location from Network Provider

}
}

So this will fetch location from GPS first (if available), else it will try to fetch location from Network Provider.

EDIT:

To make it better, I'll post a snippet. Consider it is in try-catch:

boolean gps_enabled = false;
boolean network_enabled = false;

LocationManager lm = (LocationManager) mCtx
.getSystemService(Context.LOCATION_SERVICE);

gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Location net_loc = null, gps_loc = null, finalLoc = null;

if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (gps_loc != null && net_loc != null) {

//smaller the number more accurate result will
if (gps_loc.getAccuracy() > net_loc.getAccuracy())
finalLoc = net_loc;
else
finalLoc = gps_loc;

// I used this just to get an idea (if both avail, its upto you which you want to take as I've taken location with more accuracy)

} else {

if (gps_loc != null) {
finalLoc = gps_loc;
} else if (net_loc != null) {
finalLoc = net_loc;
}
}

Now you check finalLoc for null, if not then return it.
You can write above code in a function which returns the desired (finalLoc) location. I think this might help.

How to get location either from GPS or NETWORK providers in android

You should first check before getting location from both. First check if location is available through gps and if not then use network to get location.
Here is a sample class which have methods for checking location.

    public class GPSTracker extends Service implements LocationListener {

private final Context context;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
public boolean canGetLocation = false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GPSTracker(Context context) {
this.context = context;
getLocation();
}

public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(!isGPSEnabled && !isNetworkEnabled) {

} else {
this.canGetLocation = true;

if (isNetworkEnabled) {

locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

if (location != null) {

latitude = location.getLatitude();
longitude = location.getLongitude();
}
}

}

if(isGPSEnabled) {
if(location == null) {
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();
}
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
}

return location;
}

public void stopUsingGPS() {
if(locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}

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

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

return longitude;
}

public boolean canGetLocation() {
return this.canGetLocation;
}

public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

alertDialog.setTitle("GPS is settings");

alertDialog.setMessage("Turn on your GPS to find nearby helpers");

alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});

alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

alertDialog.show();
}

@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

}

Android get location from GPS

You might want to consider using Network Location as well as GPS Location, as many users just keep the GPS radio off to conserve battery.
With Location Services set in "Battery Saving" mode, the device can get accurate geo-location data using Wi-Fi if available.

If you use Network Location data as well as GPS radio data, you would have a much better chance of getting location data when you need it.

However, if Location Services were disabled completely prior to being turned on in order to post something using your app, then you would need to register a Location Listener to get the first instance of location data.

Take a look at this post to see how to use both Network and GPS Location:
Android Location Manager, Get GPS location ,if no GPS then get to Network Provider location

GPS provider not showing current location at first time

Everything you code is write, But you are not calling LocationManager on starting of Application. Just un comment the following line from your code from your onCreate() method.

//Location location = locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this);

Uncomment above line and make following change for provider

Location location = locationManager.requestLocationUpdates(provider, 5000L, 10F, this);

Android LocationManager network provider returns null

Please do NOT use this code. It's bad. It has a lot of errors. Also, getLastKnownLocation will return null if it doesn't have a location yet. Which it always will if nobody on the phone is using requestUpdates.

Your code is taken from a class that was posted on a very old thread on here called GPSTracker. I've been trying to kill that code for months- it causes far more problems than it helps. If you want better example code, try http://gabesechansoftware.com/location-tracking/ which is a blog post I wrote about how bad that code is. It will show you the correct way to do it, and explains some of what's wrong with that code.

Android Location From network when No GPS returns null

The location listener method returns null until it founds a lock on a location.The Network provider location takes few seconds or even minutes to have a lock on a location so it is required to wait until there is a lock on a location.



Related Topics



Leave a reply



Submit