语言

Menu
Sites
Language
GPS, battery usage in Gear S

Hi,

First of all I would like to know what is best concerning battery usage/life:

call again "getCurrentPosition" after getting an error or a location update or just use watchPosition. I am developing my own running application for Gear S and it seems to me that "watchPosition" is overkill, i do not need an update every second or less.

on the other hand, getCurrentPosition in combination with setTimeout does not work properly:

    function get_coordinates() 
    {
        if (navigator && navigator.geolocation && navigator.geolocation.getCurrentPosition) {
            var options = {enableHighAccuracy:true, timeout:3000};
            navigator.geolocation.getCurrentPosition(onGpsSuccess, onGpsError, options);
        }
    }        

    function onGpsSuccess(pos)
    {
        if (pos.coords.latitude === 0 && pos.coords.longitude === 0) {
            console.log('Unable to acquire your location');
        }
        else {
            if(last_lat !== 0 && last_lon !== 0) {
                calculate_distance(last_lat, last_lon, pos.coords.latitude, pos.coords.longitude);
                //calculate_distance2(last_lat, last_lon, pos.coords.latitude, pos.coords.longitude);
            }
            last_lat = pos.coords.latitude;
            last_lon = pos.coords.longitude;
        }
        var t = setTimeout(function () { get_coordinates(); }, 2000);   // wait 2 seconds for the next call
        //get_coordinates();   // get a new position immediately
    }

Calling get_coordinates inside onGpsSuccess gives me the next update immediately, like watchPosition, but if use setTimeout I get following update many seconds later if not an error.

Thanks,

Sergio

响应

2 回复
daniel kim

Hi,


According to W3C api document, you can use maximumAge and timeout in the watchPosition() method.


http://www.w3.org/TR/2013/REC-geolocation-API-20131024/


 

AVSukhov

Hello,

From Tizen Tutorial:

Create a "one-shot" position request with the getCurrentPosition() method.

The maximumAge parameter determines that if the user agent does not have cached position information that is fresher than 60000 milliseconds (1 minute), new location information is automatically obtained.

 

function oneShotFunc() 
{
   if (navigator.geolocation) 
   {
      navigator.geolocation.getCurrentPosition(successCallback, errorCallback, 
                                               {maximumAge: 60000});
   } 
   else 
   {
      document.getElementById("locationInfo").innerHTML = "Geolocation is not supported.";
   }
}