Google Maps API v3 Hiding and showing a circle bound to a marker

一世执手 提交于 2019-12-23 21:28:56

问题


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

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.


回答1:


One option is to make the circle a property of the marker (like ._myCircle), reference it in the click handler as marker._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);



回答2:


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


来源:https://stackoverflow.com/questions/13773688/google-maps-api-v3-hiding-and-showing-a-circle-bound-to-a-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!