Javascript 在谷歌地图中切换GeolocationMarker(),一旦setMap(null)完成,它将赢得';不要再打开
我试图弄明白为什么GeolocationMarker()的响应与普通标记不同 使用一个简单的标记,您可以使用marker.setMap(map)和marker.setMap(null)来打开和关闭它。但是,当我用一个GeoMarker做这件事的时候,一旦我关掉它,我就无法让它一直打开Javascript 在谷歌地图中切换GeolocationMarker(),一旦setMap(null)完成,它将赢得';不要再打开,javascript,google-maps,google-maps-api-3,google-maps-markers,Javascript,Google Maps,Google Maps Api 3,Google Maps Markers,我试图弄明白为什么GeolocationMarker()的响应与普通标记不同 使用一个简单的标记,您可以使用marker.setMap(map)和marker.setMap(null)来打开和关闭它。但是,当我用一个GeoMarker做这件事的时候,一旦我关掉它,我就无法让它一直打开 controlUI.addEventListener('click', function() { if (content == 'zoomIn') { map.setZoom(map.get
controlUI.addEventListener('click', function() {
if (content == 'zoomIn') {
map.setZoom(map.getZoom() + 1);
} else if (content == 'zoomOut') {
map.setZoom(map.getZoom() - 1);
} else if (content == 'myGeolocation') {
//If there is no GeolocationMarker, this makes one and turns it on.
if (watchRunning == 0) {
GeoMarker = new GeolocationMarker({
enableHighAccuracy: false
});
GeoMarker.setMap(map);
watchRunning = 1;
document.getElementById(content).style.backgroundColor =
'rgba(30, 144, 255,.8)';
document.getElementById("imgGeolocation").src =
"geolocation_white.png";
//If the GeolocationMarker is on, this turns it off.
} else if (watchRunning == 1) {
GeoMarker.setMap(null);
document.getElementById(content).style.backgroundColor =
'rgba(255,255,255,.7)';
document.getElementById("imgGeolocation").src =
"geolocation_black.png";
watchRunning = 2;
//If the GeolocationMarker is off, this should turn it back on.
} else {
GeoMarker.setMap(map);
document.getElementById(content).style.backgroundColor =
'rgba(30, 144, 255,.8)';
document.getElementById("imgGeolocation").src =
"geolocation_white.png";
watchRunning = 1;
};
}
})
这应该在地图上显示出来
GeoMarker.setMap(map);
这应该隐藏它
GeoMarker.setMap(null);
首先,所有这些都能正常工作。问题是当用户切换开关几次时。GeoMarker不会一直打开。我通常可以让它工作一两次,之后它就不会再打开了
有没有想过为什么会发生这种情况?我知道,将其放置在事件侦听器中并不是实际函数的理想位置,但只要背景和img发生变化,其余部分就可以工作。假设变量定义正确(否则它根本无法工作),您可以使用此库: 每次显示标记时,库都会重新运行地理定位部分(这可能需要一些时间,并且可能会产生不希望的副作用) 可能的解决方法:不要切换
映射
-属性,而是切换可见性
:
if (watchRunning == 0) {
GeoMarker = new GeolocationMarker({
enableHighAccuracy: false
});
GeoMarker.setMap(map);
watchRunning = 1;
document.getElementById(content).style.backgroundColor =
'rgba(30, 144, 255,.8)';
document.getElementById("imgGeolocation").src =
"geolocation_white.png";
//If the GeolocationMarker is on, this turns it off.
} else if (watchRunning == 1) {
GeoMarker.setMarkerOptions({visible:false});
GeoMarker.setCircleOptions({visible:false});
document.getElementById(content).style.backgroundColor =
'rgba(255,255,255,.7)';
document.getElementById("imgGeolocation").src =
"geolocation_black.png";
watchRunning = 2;
//If the GeolocationMarker is off, this should turn it back on.
} else {
GeoMarker.setMarkerOptions({visible:true});
GeoMarker.setCircleOptions({visible:true});
document.getElementById(content).style.backgroundColor =
'rgba(30, 144, 255,.8)';
document.getElementById("imgGeolocation").src =
"geolocation_white.png";
watchRunning = 1;
};
当不使用普通的Google Maps API代码时,最好提及您使用的插件/库。您在哪里声明
GeoMarker
?请发表一篇文章来说明这个问题。如果可能的话,创建一个。顺便说一句,欢迎来到SO。什么是内容
,您在哪里声明watchRunning
?很抱歉响应延迟。我最终选择了另一条路线,在地图上放置一个简单的标记时使用了watchPosition()
,切换效果很好。在将来,我会确保我的问题有更好的代码示例。感谢您提出的切换到可见性而不是取消可见性的想法。正如我在另一篇评论中提到的,我决定使用一个由watchPosition()
和getCurrentPosition
控制的简单标记来重建程序。显然,它不喜欢多次重建地理定位标记。