0

1.The below code gives me Network location but not giving GPS location
2.When I disabled my WIFI it gives network location
3.When I disabled my Network it shows last known location only
4.First of all I need to check that network is available or not
5.If Network available get the location from network
6.Otherwise I need to get the location of GPS

public class GetandGiveLocation extends Service implements LocationListener {

    private final Context mContext;

    boolean isGPSEnabled = false;

    boolean isNetworkEnabled = false;

    boolean canGetLocation = false;

    Location location;
    double latitude;
    double longitude;
    String bestProvider;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;

    private static final long MIN_TIME_BW_UPDATES = 0;

    protected LocationManager locationManager;

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

    public Location getLocation() {
        try {

            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                showSettingsAlert();
            } 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) {

                    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 boolean canGetLocation() {
        return this.canGetLocation;
    }

    @Override
    public void onLocationChanged(Location location) {

        latitude = location.getLatitude();
        longitude = location.getLongitude();
        GoogleMapViewer gmv = new GoogleMapViewer();
        SendToWebsite stw = new SendToWebsite();
        stw.execute(latitude, longitude);

    }

}
shabeer
  • 143
  • 1
  • 8

1 Answers1

0

My guess is that you might want to change

if (isNetworkEnabled) {
    ...
}
if (isGPSEnabled) {
    ...
}

To

if (isNetworkEnabled) {
    ...
}
else if (isGPSEnabled) {
    ...
}

Otherwise it (I assume) will not try WiFi before turning to GPS if both are available.

When GPS is picked, it can take some time to warm up. During this time the getLastKnownLocation might be stale and even null.

I have implemented some code using GPS that determines if and when the GPS is ready to provide valid data (have gotten a first fix). You can find the code here: Location servise GPS Force closed

The code only cares about GPS though and would have to be modified to handle network positioning too. As I recall, the original code, which I refer to in the link above, does have network positioning too, so should not be that difficult to add again.

Hope this helps - happy coding

Community
  • 1
  • 1
cYrixmorten
  • 7,110
  • 3
  • 25
  • 33