Couldn't get user location

三世轮回 提交于 2019-12-02 08:22:38
MysticMagicϡ

As you said, you are not even getting toast of "Getting location Via Network". So your execution is not going inside the if block of

if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {

}

So make sure you have checked

Google's location services - Checked

in your device Settings.

Settings > location services > Google's location services and Access Location.

Then you should get location from LocationManager. Else rest of your code looks fine.

Hope this helps.

Reference link

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

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

or

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000*60*1, this);

You need to keep in mind about permissions :

1.If you want to use both NETWORK_PROVIDER and GPS_PROVIDER usepermission mentioned below.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

2.if want to use only NETWORK_PROVIDER use

<uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />

3.A very common mistake can be done by normal human being. don`t forget to give Internet Permission

<uses-permission android:name="android.permission.INTERNET" />

4.The first fix for location can be long enough, the //locationManager.getLastKnownLocation(locationProvider) gives you cached location It can be null also.

You can request location based on certain Criteria:

// The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 5; // 5 minute


Criteria criteria = new Criteria();
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(false);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            String providerFine = locationManager.getBestProvider(criteria,
                    true);

            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String providerCoarse = locationManager.getBestProvider(criteria,
                    true);

            if (providerCoarse != null) {

                locationManager.requestLocationUpdates(providerCoarse,
                        MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
                        this);
            }
            if (providerFine != null) {

                locationManager.requestLocationUpdates(providerFine,
                        MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES,
                        this);
            }

After getting location you can check for best location fix Below method is given out here: http://developer.android.com/guide/topics/location/strategies.html

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!