Difference between LocationRequest setInterval (long millis) and LocationRequest setFastestInterval (long millis)

前端 未结 4 1807
一个人的身影
一个人的身影 2021-01-31 06:52

I am writing my own background location updates for interval of every 5 minutes in android. I would like to know the difference between setInterval and setFastestInterval

4条回答
  •  鱼传尺愫
    2021-01-31 07:28

    I am writing my own background location updates for interval of every 5 minutes in android. I would like to know the difference between setIntervaland setFastestInterval

    Assume that the setFastestInterval(); has higher priority for requesting a Location. To whatever app you set the setFastestInterval(); it will be that app that will be executed first (even if other apps are using LocationServices).

    ex: If APP1 has setFastestInterval(1000 * 10) and APP2 has setInterval(1000 * 10), both APPS have same request interval. But it is the APP1 that will make the first request. (this is what i have understood, the answer is not correct maybe)

    When I setInterval to 5 mins and setFastestInterval to 2 mins. The location update is called every 2 mins.

    If you are using setFastestInterval() together with the setInterval() the app will try to make a request for the time given in the setFastestInterval(), that's why your app makes a request every 2mins.

    Also: Is there an inbuilt function to check the location updates only if the distances of the first update are more than 20meters with the second update?

    For Making request every 20 meters you can create a LocationModel

        public class LocationModel {
        private double latitude;
    
        private double longitude;
        public LocationModel(){
        }
    
        public double getLatitude() {
            return latitude;
        }
    
        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }
    
        public double getLongitude() {
            return longitude;
        }
    
        public void setLongitude(double longitude) {
            this.longitude = longitude;
        }
    }
    

    and in the first request you set the lat and long to current location (using getLastLocation();)

    then onLocationChanged() you get the data from the object and compare with the new Current Location

    float distanceInMeters = distFrom((float)locationObj.getLatitude(), (float)locationObj.getLongitude(), (float)mCurrentLocation.getLatitude(), (float)mCurrentLocation.getLongitude());
    

    using this function which is also a suggestion of users of SO

    public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
        double earthRadius = 6371; //kilometers
        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lng2-lng1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLng/2) * Math.sin(dLng/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        float dist = (float) (earthRadius * c);
    
        return dist;
    }
    

提交回复
热议问题