WatchClear 方法 html5,javascript - Html5 Geolocation clearWatch() is not responding - Stack Overflow...

I compile and adjust this demo script for learning tracking with geolocation from parts of other scripts. So far so good... but the functions clearWatch() to stop searching or tracking are not responding because the alert() after it is not showing up.

Something I forgot?

html { height: 100% }

body { height: 100%; margin: 0px; padding: 0px }

#map_canvas { height: 100% ; width:100%;}

var previousPosition = null;

var startId;

var trackId;

function initialize() {

document.getElementById("info").innerHTML = "Je positie aan het zoeken...";

if (navigator.geolocation) {

startId = navigator.geolocation.watchPosition(function(position) {

var startPos = position;

var startAccuracy = startPos.coords.accuracy;

document.getElementById("info").innerHTML = "In de buurt... ca." + startAccuracy + " meter...";

if(startAccuracy < 3) {

document.getElementById("info").innerHTML = "Gevonden... binnen " + startAccuracy + " meter...";

map = new google.maps.Map(document.getElementById("map_canvas"), {

zoom: 18,

center: new google.maps.LatLng(startPos.coords.latitude, startPos.coords.longitude),

mapTypeId: google.maps.MapTypeId.ROADMAP

});

var marker = new google.maps.Marker({

position: new google.maps.LatLng(startPos.coords.latitude, startPos.coords.longitude),

map: map

});

navigator.geolocation.clearWatch(startId);

document.getElementById("info").innerHTML = "Tracking start over 5 seconden...";

setTimeout(function(){startTracking()},5000);

}

}, function(error) {

alert("Error code: " + error.code);

// error.code can be:

// 0: unknown error

// 1: permission denied

// 2: position unavailable (error response from locaton provider)

// 3: timed out

}, {maximumAge:1000, enableHighAccuracy: true});

}

else {

alert("GeoLocatie wordt niet ondersteund!");

}

}

function startTracking() {

document.getElementById("info").innerHTML = "Tracking is gestart...";

trackId = navigator.geolocation.watchPosition(successCallback, null, {maximumAge:1000, timeout:60000, enableHighAccuracy:true});

function successCallback(position){

map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));

if (previousPosition){

var newLineCoordinates = [

new google.maps.LatLng(previousPosition.coords.latitude, previousPosition.coords.longitude),

new google.maps.LatLng(position.coords.latitude, position.coords.longitude)];

var newLine = new google.maps.Polyline({

path: newLineCoordinates,

strokeColor: "#FF0000",

strokeOpacity: 1.0,

strokeWeight: 2

});

newLine.setMap(map);

}

previousPosition = position;

};

}

function stopZoeken() {

navigator.geolocation.clearWatch(startId);

alert("Zoeken gestopt!");

}

function stopTracking() {

navigator.geolocation.clearWatch(trackId);

alert("Tracking gestopt!");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值