Google Maps API v3隐藏并显示与标记绑定的圆圈 [英] Google Maps API v3 Hiding and showing a circle bound to a marker
问题描述
我已经使用google map api v3成功绑定了一个圈子到我的标记。我知道这一点,因为如果我让标记拖动,圆圈也会移动。
I have successfully bound a circle to my marker using google map api v3. I know this because if I make the marker dragable the circle moves as well.
如果点击标记,我该如何参考圆圈。我需要显示圆圈,如果不可见或反之亦然。
How can I refer to the circle if the marker is clicked. I need to show the circle if not visible or vice-versa.
以下是创建标记和圆圈的代码
Here is the code to create the marker and circle
var markerOptions = {
title: title,
icon: markerImage,
shadow: markerShadow,
position: latlng,
map: map
}
var marker = new google.maps.Marker(markerOptions);
// Add a Circle overlay to the map.
var circle = new google.maps.Circle({
map: map,
radius: 50*1609.34,// 50 MI
visible: false
});
//circle.bindTo('map', marker);
circle.bindTo('center', marker, 'position');
我在stackoverflow上找到了一个答案,这让我认为我需要做rem'd out map绑定以及中心绑定,但没有奏效。
I found an answer on stackoverflow that led me to think I needed to do the rem'd out map binding as well the center binding, but that did not work.
这是我的标记点击事件。
Here is my click event for the marker.
google.maps.event.addListener(marker, "click", function() {
var infowindowOptions = {
content: html
}
var infowindow = new google.maps.InfoWindow(infowindowOptions);
cm_setInfowindow(infowindow);
infowindow.open(map, marker);
marker.setIcon(markerImageOut);
marker.circle({visible: true});
任何想法。与刚刚点击或挖出的标记的界限圆。
Any ideas. I need to interact with the bound circle of the marker that was just clicked or moused over.
推荐答案
一种选择是使圆圈成为(例如._myCircle),在click处理程序中以marker._myCircle引用它。
One option is to make the circle a property of the marker (like ._myCircle), reference it in the click handler as marker._myCircle.
将圆圈添加为标记的_myCircle属性:
Add the circle as the _myCircle property of marker:
var circle = new google.maps.Circle({
map: map,
radius: 50*1609.34,// 50 MI
visible: false
});
circle.bindTo('center', marker, 'position');
marker._myCircle = circle;
要切换它,请使用类似(未测试)的内容:
To toggle it use something like (not tested):
if(marker._myCircle.getMap() != null) marker._myCircle.setMap(null);
else marker._myCircle.setMap(map);
这篇关于Google Maps API v3隐藏并显示与标记绑定的圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!