在 Google 地图中切换 GeolocationMarker(),一旦 setMap(null) 完成,它就不会重新打开
Toggling GeolocationMarker() in Google Maps, once setMap(null) is completed it won't turn back on
我想弄清楚为什么 GeolocationMarker() 的响应与普通标记不同。
使用简单的标记,您可以使用 marker.setMap(map) 和 marker.setMap(null) 打开和关闭它。但是,当我使用 GeoMarker 执行此操作时,一旦将其关闭,我就无法始终如一地重新打开它。
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);
一开始,所有这些都正常工作。问题是当用户切换 on/off 几次时。 GeoMarker 不会持续打开。我通常可以让它工作一两次,之后它就不会再打开了。
关于为什么会发生这种情况有什么想法吗?我知道将它放在事件侦听器中并不是实际功能的理想位置,但就背景和 img 更改而言,它的其余部分可以正常工作。
假设变量定义正确(否则它根本无法工作)并且你使用这个库:https://google-maps-utility-library-v3.googlecode.com/svn/trunk/geolocationmarker/docs/reference.html
似乎每次您显示标记时,库都会重新运行 地理定位部分(这可能需要一些时间并且可能会产生不良副作用)。
可能的解决方法:不要切换 map
-属性,而是切换 visibility
:
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;
};
我想弄清楚为什么 GeolocationMarker() 的响应与普通标记不同。
使用简单的标记,您可以使用 marker.setMap(map) 和 marker.setMap(null) 打开和关闭它。但是,当我使用 GeoMarker 执行此操作时,一旦将其关闭,我就无法始终如一地重新打开它。
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);
一开始,所有这些都正常工作。问题是当用户切换 on/off 几次时。 GeoMarker 不会持续打开。我通常可以让它工作一两次,之后它就不会再打开了。
关于为什么会发生这种情况有什么想法吗?我知道将它放在事件侦听器中并不是实际功能的理想位置,但就背景和 img 更改而言,它的其余部分可以正常工作。
假设变量定义正确(否则它根本无法工作)并且你使用这个库:https://google-maps-utility-library-v3.googlecode.com/svn/trunk/geolocationmarker/docs/reference.html
似乎每次您显示标记时,库都会重新运行 地理定位部分(这可能需要一些时间并且可能会产生不良副作用)。
可能的解决方法:不要切换 map
-属性,而是切换 visibility
:
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;
};