html5中watchPosition,HTML5地理定位watchPosition只调用一次

几个月前我一直在做类似的事情.我记得有同样的问题.也许这会有所帮助.

$(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));

});

这是你在寻找的

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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值