【发布时间】:2017-07-07 01:52:38
【问题描述】:
我在 SO 上查看了多个类似的帖子,但还没有找到答案。我有一个谷歌地图应用程序,当你放大标记时,它会从一个图标变为一个圆圈。它工作得很好。标记图标被圆形对象取代。但是,我也希望它反向工作:当你缩小时,我想删除圆圈并将其替换为图标。我可以让图标“重新出现”,但我想不出一种方法来获取对绑定到标记的圆形对象的引用,以便将其删除。
这不是我使用的完整代码,而是为了满足对 MINIMAL 而不是完整的请求,这会产生问题:
var marker;
createamarker();
removeCircle();
function createamarker(){
marker = new google.maps.Marker({
position: location,
map: map,
icon: icon,
// Add some custom properties
obscure:obscure,
originalpin: icon
});
// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
map: map,
radius: 1000, // in metres
fillColor: '#AA0000'
});
// Bind it to the marker
circle.bindTo('center', marker, 'position');
}
我还有第二个功能是应该删除圆圈:
function removeCircle(){
// remove whatever is there
marker.setMap(null);
var icon = {
url: marker.originalpin,
scaledSize: new google.maps.Size(22,32)
}
// reset the marker icon
marker.icon = icon;
//sets the marker back
marker.setMap(map);
// NOW REMOVE the circle:
// So at this point I am stuck. I have bound a circle to
// the marker but in order to REMOVE the circle I need a
// reference to it. Other SO postings suggest acting on the
// circle object directly like so:
circle.setMap(null);
// but the "circle" doesn't exist here. It was bound to the marker in another function. I need a reference to the circle that was bound to the marker so I can act on it.
}
【问题讨论】:
标签: google-maps google-maps-api-3 geometry unbind