1

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.

2 Answers2

2

The watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.

This method returns a watch ID value then can be used to unregister the handler by passing it to the clearWatch() method.

To obtain the user's current location, you can call the getCurrentPosition() method. This initiates an asynchronous request to detect the user's position, and queries the positioning hardware to get up-to-date information. When the position is determined, the defined callback function is executed. You can optionally provide a second callback function to be executed if an error occurs. A third, optional, parameter is an options object where you can set the maximum age of the position returned, the time to wait for a request, and if you want high accuracy for the position.

If the position data changes (either by device movement or if more accurate geo information arrives), you can set up a callback function that is called with that updated position information. This is done using the watchPosition() function, which has the same input parameters as getCurrentPosition(). The callback function is called multiple times, allowing the browser to either update your location as you move, or provide a more accurate location as different techniques are used to geolocate you. The error callback function, which is optional just as it is for getCurrentPosition(), can be called repeatedly.

For more information check this documentation

Check also this SO question.

Community
  • 1
  • 1
KENdi
  • 7,576
  • 2
  • 16
  • 31
-1

Setting up members to be at where they should.rje blue dot i had noticed was way off for whomever to come find me. Working on a update that is fixing the correct coding .

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '23 at 19:04