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!");
}