3

我已经使用 google map api v3 成功地将一个圆圈绑定到我的标记。我知道这一点,因为如果我使标记可拖动,圆圈也会移动。

如果单击标记,我如何参考圆圈。如果不可见,我需要显示圆圈,反之亦然。

这是创建标记和圆圈的代码

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 地图绑定以及中心绑定,但这不起作用。

这是我的标记点击事件。

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});

有任何想法吗。我需要与刚刚单击或鼠标悬停的标记的绑定圆圈进行交互。

4

2 回答 2

5

一种选择是使圆成为标记的属性(如 ._myCircle),在单击处理程序中将其作为 marker._myCircle 引用。

添加圆圈作为标记的 _myCircle 属性:

var circle = new google.maps.Circle({
  map: map,
  radius: 50*1609.34,// 50 MI
  visible: false
});
circle.bindTo('center', marker, 'position');
marker._myCircle = circle;

要切换它使用类似的东西(未测试):

if(marker._myCircle.getMap() != null) marker._myCircle.setMap(null);
else marker._myCircle.setMap(map);
于 2012-12-08T02:02:01.427 回答
1
var rad =".$this->conf['radius'] * 1000 ."; //convert km to meter
var populationOptions = {
   strokeColor: '#FF0000',
   strokeOpacity: 0.8,
   strokeWeight: 1,
   fillColor: '#FF0000',
   fillOpacity: 0.35,
   map: map,//map object
   center: new google.maps.LatLng($corr_match[0], $corr_match[1]),//center of circle
   radius: rad
};
var cityCircle = new google.maps.Circle(populationOptions);
于 2014-10-01T10:49:06.513 回答