地理定位WebApi获得速度

时间:2017-03-05 14:43:58

标签: javascript geolocation

我试图通过Geolocation Webapi获取设备的速度,但使用此代码不会获得任何价值:

var x = document.getElementById("speed");
var xkmh = document.getElementById("speed2");
var y = document.getElementById("accuracy");
var z = document.getElementById("timestamp");
var longitude = document.getElementById("longitude");
var latitude = document.getElementById("latitude");

function testLocation() {
    if (navigator.geolocation) {
        console.log("geolocation available");
        getLocation();
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
        alert("gps not supported");
    }
}

function getLocation () {
    navigator.geolocation.getCurrentPosition(showPosition);
    setTimeout(function() {getLocation()}, 500);
}


function showPosition(position) {
        console.log(position);
        x.innerHTML = position.speed + " m/s";
        xkmh.innerHTML = position.speed * 3.6 + " km/h";
        y.innerHTML = position.accuracy + " accuracy";
        z.innerHTML = position.timestamp + " timestamp";
        longitude.innerHTML = position.coords.longitude;
        latitude.innerHTML = position.coords.latitude;
        console.log("run");
}

testLocation();

它是否仅适用于 showPosition()

0 个答案:

没有答案