【发布时间】:2015-11-09 01:33:19
【问题描述】:
我正在使用标记在我的网站上显示带有当前用户位置的 Google 地图(然后,我将添加其他功能)。我能够做到:每次用户移动时,都会在用户在地图上的新位置上插入另一个标记,但不会删除前一个标记,所以我要添加越来越多的标记,我可以' t 在刷新用户位置之前删除前一个。我尝试使用计时器和其他策略但没有成功:marker.setVisible(false)、marker.setMap(null)。
代码:
<!DOCTYPE html>
<html>
<head>
<title>Geolocation</title>
<style>
body,html {height:100%}
#map {width:100%;height:100%;}
</style>
<script src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script>
var map;
function initGeolocation() {
if (navigator && navigator.geolocation) {
var watchId = navigator.geolocation.watchPosition(successCallback, errorCallback, {enableHighAccuracy:true,timeout:5000,maximumAge:0});
} else {
console.log('Geolocation not suported');
}
}
function errorCallback() {}
function successCallback(position) {
var myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
if(map == undefined) {
var myOptions = {
zoom: 18,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
}
else map.panTo(myLatlng);
markerUser = new google.maps.Marker({
position: myLatlng,
map: map,
icon: 'img/iconFox.png',
title:"You're here!"
});
}
</script>
</head>
<body onload="javascript:initGeolocation()">
<div id="map">
</div>
</body>
</html>
【问题讨论】:
-
首先是
marker.setMap(null);,请确保您在发帖之前彻底阅读了文档。 -
谢谢你,你是对的,但这是一个错字。当然,我使用了marker.setMap(null)。我已经更正了帖子。
标签: javascript html google-maps