Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 geolocation watchPosition getting called just once

I am creating an android application using html 5 and I want to access latlong from the user as the user moves. I have used watchPostion but it isgiving me user location just once.

    var watchID;
var geoLoc;

function showLocation(position) {
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;

    var ll=new google.maps.LatLng(latitude, longitude);
    map.setCenter(ll);

  console.log("Latitude : " + latitude + " Longitude: " + longitude);
}

function errorHandler(err) {
  if(err.code == 1) {
    console.log("Error: Access is denied!");
  }else if( err.code == 2) {
    console.log("Error: Position is unavailable!");
  }
    }
    if(navigator.geolocation){
      // timeout at 60000 milliseconds (60 seconds)
      var options = {timeout:60000};
      geoLoc = navigator.geolocation;
      watchID = geoLoc.watchPosition(showLocation, 
                                     errorHandler,
                                     options);


   }else{
      console.log("Sorry, browser does not support geolocation!");
   }
like image 358
Naeem Shaikh Avatar asked May 16 '14 17:05

Naeem Shaikh


People also ask

Why is my Geolocation not accurate?

The accuracy of your location may vary depending on your environment. The GPS sometimes doesn't work well when you are inside a house or a building. That is because buildings themselves are potential obstacles that may hinder satellite connection.

What is Geolocation watchPosition in HTML5?

The Geolocation method 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.

Which method is used to stop the watchPosition method?

The watchPosition method returns a unique transaction ID (number) associated with the asynchronous call. Use this ID to cancel the watchPosition call and to stop receiving location updates.


1 Answers

I've been working on something similar a few months ago. I remember having the same issue. Maybe this will help.

$(document).ready(function(){  
    initLocationProcedure();
}

function initLocationProcedure() {
    map = new google.maps.Map(document.getElementById('map_canvas'), {
          zoom : 17
    });

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(displayAndWatch, locError, {
            enableHighAccuracy : true,
            timeout : 60000,
            maximumAge : 0
        });
    } else {
        alert("Your phone does not support the Geolocation API");
    }
}

function displayAndWatch(position) {
    // set current position
    setUserLocation(position);
    // watch position
    watchCurrentPosition();
}

function setUserLocation(pos) {
    // marker for userLocation
    userLocation = new google.maps.Marker({
           map : map,
           position : new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude),
           title : "You are here",
           icon : "../img/user-location.svg",
    // scroll to userLocation
    map.panTo(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
});

Here is what you are looking for

function watchCurrentPosition() {
    var positionTimer = navigator.geolocation.watchPosition(function(position) {
        setMarkerPosition(userLocation, position);
        map.panTo(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
    });
}

function setMarkerPosition(marker, position) {
     marker.setPosition(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
     console.log(position);
}

Fiddle
http://jsfiddle.net/XEFks/

like image 73
boortmans Avatar answered Oct 12 '22 11:10

boortmans