我想弄清楚为什么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);
一开始,所有这些东西都能正常工作。问题是当用户切换开/关几次。GeoMarker不会一直打开。我通常可以让它工作一两次,之后它就不会再打开了。
对为什么会发生这种情况有什么想法吗?我知道将它放在事件侦听器中并不是实际功能的理想位置,但是它的其余部分可以在背景和图像更改中工作。
假设变量定义正确(否则根本无法工作),并且使用以下库: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;
};