Below is the code I have used which finds the user's position.
if (navigator.geolocation) navigator.geolocation.getCurrentPosition(function(pos) {
me = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
infoWindow.setPosition(me);
infoWindow.setContent('Location found.');
},
function(error) {
handleLocationError(true, infoWindow, map.getCenter());
});
else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(me);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
}
I have tried to add in the watch position function below but it doesn't work, I am not sure how it works.
var watchID = navigator.geolocation.watchPosition(function(position) {
(position.coords.latitude, position.coords.longitude);
});
function getLocationUpdate(){
if(navigator.geolocation){
// timeout at 60000 milliseconds (60 seconds)
var options = {timeout:60000};
geoLoc = navigator.geolocation;
watchID = geoLoc.watchPosition(showLocation, errorHandler, options);
}
else{
alert("Sorry, browser does not support geolocation!");
}
If somebody could help me to watch the user's position on a map I would really appreciate it! Thank you very much.