Android get gps location by network or gps

mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

Location gpsLocation = null;
Location networkLocation = null;
mLocationManager.removeUpdates(listener);
        
gpsLocation = requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, R.string.not_support_gps);
networkLocation = requestUpdatesFromProvider(LocationManager.NETWORK_PROVIDER, R.string.not_support_network);

if (gpsLocation != null && networkLocation != null) {
    updateUILocation(getBetterLocation(gpsLocation, networkLocation)); //not implement get better location function
} else if (gpsLocation != null) {
    updateUILocation(gpsLocation);
} else if (networkLocation != null) {
    updateUILocation(networkLocation);
}

private Location requestUpdatesFromProvider(final String provider, final int errorResId) {
    Location location = null;
    if (mLocationManager.isProviderEnabled(provider)) {
        mLocationManager.requestLocationUpdates(provider, TEN_SECONDS, TEN_METERS, listener);
        location = mLocationManager.getLastKnownLocation(provider);
    } else {
        Toast.makeText(this, errorResId, Toast.LENGTH_LONG).show();
    }
    return location;
}

0 意見: