简体   繁体   中英

Not able to get accurate location on android device

We have developed a delivery app like ola and uber. Where rider can take a order and deliver to customer. We have created a android service (Background Service) in that we have initialized our location variables. We are getting location via 3 methods , Network Provider , Gps Provider , Google Fused location api. All three we have used to get a location , when we get a location it store to our member variable( later its used in side project) and the same location is saved to our server against that rider. We are facing 2 or 3 problems specifically. 1.Some time user location is correct and within a 20-30 seconds we got some wrong location with accuracy say > 50 or 100. 2.Some time location got stucked for several minutes or for prolong hour.

We are using specifically location strategy prescribed by Google. below is the sample.

private static final int TWO_MINUTES = 1000 * 60 * 2;

/** Determines whether one Location reading is better than the current Location fix
 * @param location  The new Location that you want to evaluate
 * @param currentBestLocation  The current Location fix, to which you want to compare the new one
 */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
        // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
        return provider2 == null;
    }
    return provider1.equals(provider2);
}


if (isMoreAccurate)
{
    return true;
}
else if (isNewer && !isLessAccurate) 
{
    return true;
}
else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) 
{
    return true;
}

return false;

I am having no clue what to do next.

I have integrate Network Provide , GPS provider & fused location provider. I am getting location but a less accurate. i also maintained a list of last 10 successive locations. When i receive 11th location i searched to the list and if found all location is same (that means location got stucked) then i cleared my list and dropped the location connection via stopping and restarting the service again with new connections.

Android has restrictions for getting location updates from background services. You either have to get the location from foreground activity or change your service to a Foreground Service with a pinned notification to get periodic location updates.

Location accuracy issues could also be due to user not settings high accuracy location mode in the device. This will check if location enabled in user device. Dialog will be shown if location is enabled but high accuracy not enabled.

private static final int REQUEST_CHECK_SETTINGS = 001;

private void displayLocationSettingsRequest(Context context) {
        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API).build();
        googleApiClient.connect();

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(10000);
        locationRequest.setFastestInterval(10000 / 2);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);

        PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                final Status status = result.getStatus();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        //Location settings satisfied
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        //Location settings are not satisfied. Show the user a dialog to upgrade location settings
                        try {
                            status.startResolutionForResult(Activity.this, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            LogUtil.i("PendingIntent unable to execute request.");
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        //Location settings are inadequate, and cannot be fixed here. Dialog not created.
                        Toast.makeText(Activity.this, "Error enabling location. Please try again", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM