HTML5 geolocation watchPosition getting called just once -
i creating android application using html 5 , want access latlong user user moves. have used watchpostion isgiving me user location 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 denied!"); }else if( err.code == 2) { console.log("error: position unavailable!"); } } if(navigator.geolocation){ // timeout @ 60000 milliseconds (60 seconds) var options = {timeout:60000}; geoloc = navigator.geolocation; watchid = geoloc.watchposition(showlocation, errorhandler, options); }else{ console.log("sorry, browser not support geolocation!"); }
i've been working on similar few months ago. remember having same issue. maybe 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 not support geolocation api"); } } function displayandwatch(position) { // set current position setuserlocation(position); // watch position watchcurrentposition(); } function setuserlocation(pos) { // marker userlocation userlocation = new google.maps.marker({ map : map, position : new google.maps.latlng(pos.coords.latitude, pos.coords.longitude), title : "you here", icon : "../img/user-location.svg", // scroll userlocation map.panto(new google.maps.latlng(pos.coords.latitude, pos.coords.longitude)); });
here 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/
Comments
Post a Comment