sadelbrid sadelbrid - 4 months ago 8
Android Question

LocationManager says I'm in Anttarctica

Obviously I'm not. Here is what I'm using to get location.

SingleShotLocationProvider.requestSingleUpdate(getContext(),
new SingleShotLocationProvider.LocationCallback() {
@Override
public void onNewLocationAvailable(SingleShotLocationProvider.GPSCoordinates location) {
//use location.longitude and location.latitude
}
});


And here is the class I use above:

public class SingleShotLocationProvider {

public interface LocationCallback {
void onNewLocationAvailable(GPSCoordinates location);
}

public static void requestSingleUpdate(final Context context, final LocationCallback callback) {
final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

//If network is available
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
try {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override public void onProviderEnabled(String provider) {}
@Override public void onProviderDisabled(String provider) {}
}, null);
}
catch (SecurityException e){
Log.e("Location", "security exception");
}
}
//If GPS is enabled
else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
try {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
callback.onNewLocationAvailable(new GPSCoordinates(location.getLatitude(), location.getLongitude()));
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override public void onProviderEnabled(String provider) {}
@Override public void onProviderDisabled(String provider) {}
}, null);
}
catch (SecurityException e){
Log.e("Location", "security exception");
}
}
else{
Toast.makeText(context, "No network or GPS available. Try again later.", Toast.LENGTH_LONG).show();
}
}


// consider returning Location instead of this dummy wrapper class
public static class GPSCoordinates {
public float longitude = -1;
public float latitude = -1;

public GPSCoordinates(float lon, float lat) {
longitude = lon;
latitude = lat;
}

public GPSCoordinates(double lon, double lat) {
longitude = (float) lon;
latitude = (float) lat;
}
}
}


Anyone have any ideas on what's going on here? Also, for some reason I can only get the location when GPS is enabled, and not over my network/wifi.If I try disabling GPS, both the if and elif conditions fail even though I have a good network connection. Thoughts?

Answer

You are passing the parameters in the incorrect order. On the constructor of GPSCoordinates you expect Longitude and Latitude, in this order. And when you create the GPSCoordinates you pass Latitude and Longitude, in the reversed order.

Change your GPSCoordinates to this:

public static class GPSCoordinates {
    public float longitude = -1;
    public float latitude = -1;

    public GPSCoordinates(float lat, float lon) {
        latitude = lat;
        longitude = lon;
    }

    public GPSCoordinates(double lat, double lon) {
        latitude = (float) lat;
        longitude = (float) lon;
    }
}
Comments