【问题标题】:Triangulating Position vs. GPS on Phone Browsers三角定位与手机浏览器上的 GPS
【发布时间】:2014-07-14 21:56:55
【问题描述】:

我正在尝试编写一个从 JavaScript 收集用户位置的应用程序。我目前正在使用 Geolocation API 从笔记本电脑上进行调试,并且证明它可以在给定的时间内工作。它每隔几秒钟广播一次我的位置,然后在我移动 5-10 秒后重新计算。事实证明,出于某种原因,这会占用我的大量电池/CPU。如果我要在手机上使用这个应用程序,它会通过 GPS 广播我的位置还是通过本地蜂窝塔对它的位置进行三角测量?

var latitude, longitude, accuracy, angle;
function setGeolocation(prevID) {
var geoID = window.navigator.geolocation.watchPosition(getPosition,handleErrors,{maximumAge: 1000, enableHighAccuracy: true});
//Pass the ID's to the seeMyGeoID function.
seeMyGeoID(geoID,prevID);
function getPosition(position){
    latitude = position.coords.latitude;
    longitude = position.coords.longitude;
    accuracy = position.coords.accuracy;
    angle = position.coords.heading;
    console.log(latitude+"  "+longitude+"  "+accuracy+"  "+angle);
};
function handleErrors(err){
    if (err.code == 1){
        console.log("PERMISSION_DENIED");
    }
    if (err.code == 2){
        console.log("POSITION_UNAVAILABLE");
    }
    if (err.code == 3){
        console.log("TIMEOUT");
    }
};

};

function seeMyGeoID(geolocationID,previousID){
if(geolocationID != previousID){
    previousID = geolocationID;
    //Wait 1 second.
    setTimeout(function(){
        navigator.geolocation.clearWatch(previousID);
        navigator.geolocation.clearWatch(previousID+1);
        setGeolocation(previousID);
    },1000);
}

};

//Call function for the first time.

setGeolocation(0);


//I want this code to be able to take a position every 0.5-1 second, or in real time (but that may be pushing it).

【问题讨论】:

    标签: javascript html gps geolocation location


    【解决方案1】:

    这取决于您在enableHighAccuracy 选项中的选择,并且取决于手机的浏览器实现。

    https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions.enableHighAccuracy

    不同的实现以不同的方式解释“高精度”。这只是意味着浏览器会尝试获得高精度,即使它需要牺牲电池并且需要很长时间。多高才够高不是你决定的。您将无法控制它是 GPS 还是 Wi-Fi/蜂窝三角测量。实现为您决定。

    获取地理位置总是会消耗电池寿命。如果watchPosition 对您来说成本太高,您可以考虑将getCurrentPosition 用于您自己的策略。

    【讨论】:

    • 这是有道理的。我还没有能力从手机调试/测试它。我正在尝试了解如何指导实现,以便它使用 GPS 作为首选,这样编译速度和电池寿命就不会成为负担(希望如此)。谢谢你,陈
    猜你喜欢
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多