Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance parameter

倖福魔咒の 提交于 2019-12-11 03:16:03

问题


I researched online and saw that Location Manager.requestLocationUpdates method and saw that it took an argument of minDistance. The definition that the site(http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/) gave me for that argument was "minimum distance interval for notifications" with an example of 10 meters. Can anyone clarify what that means? Everytime i move 10 meters with a phone, i get an gps update?


回答1:


Yes, essentially this means that if the platform observes your current position as being more than minDistance from the last location that was saved by the platform, your listener will get notified with the updated position. Note that these two positions don't necessarily need to be sequential (i.e., there could be a number of small displacements that eventually add up to minDistance, and the location that finally exceeds the threshold will be the one reported to the app).

The actual platform code can be seen on Github, which I've also pasted below:

private static boolean shouldBroadcastSafe(
        Location loc, Location lastLoc, UpdateRecord record, long now) {
    // Always broadcast the first update
    if (lastLoc == null) {
        return true;
    }

    // Check whether sufficient time has passed
    long minTime = record.mRequest.getFastestInterval();
    long delta = (loc.getElapsedRealtimeNanos() - lastLoc.getElapsedRealtimeNanos())
            / NANOS_PER_MILLI;
    if (delta < minTime - MAX_PROVIDER_SCHEDULING_JITTER_MS) {
        return false;
    }

    // Check whether sufficient distance has been traveled
    double minDistance = record.mRequest.getSmallestDisplacement();
    if (minDistance > 0.0) {
        if (loc.distanceTo(lastLoc) <= minDistance) {
            return false;
        }
    }
...

Note that minDistance parameter only affects when your app gets notified if the value is greater than 0.

Also please be aware that with all positioning systems there is a significant level of error when calculating locations, so with small minDistance values you may get notified frequently, but these notifications may be error in positioning calculations, not true user movement.



来源:https://stackoverflow.com/questions/24354512/clarification-on-min-distance-on-locationmanager-requestlocationupdates-method

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