javascript 谷歌地图圈:移动时如何触发事件以及如何获取新中心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10265752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google maps circle: how to trigger an event when moved and how to obtain the new center
提问by banditKing
So I was able to make a circle object as an overlay on my google map v3. I set its editable property to true. The next thing I wanted to do was get the coordinates of the center if the user moves the circle. For this I would need some kind of method thats triggered in response to the event. I thought I had this all set up in the initialize function as seen below. However, Im not getting any alert boxes. So Im assuming this functions in response to the events are not getting triggered.
所以我能够在我的谷歌地图 v3 上制作一个圆形对象作为叠加层。我将其可编辑属性设置为 true。我想做的下一件事是在用户移动圆时获取中心坐标。为此,我需要某种响应事件而触发的方法。我以为我已经在初始化函数中设置了这一切,如下所示。但是,我没有收到任何警报框。所以我假设这个响应事件的功能没有被触发。
function initialize() {
cityCenterLatLng = new google.maps.LatLng(cLat, cLong);
options = {
center : cityCenterLatLng,
zoom : 15,
mapTypeId : google.maps.MapTypeId.ROADMAP,
disableDefaultUI : false,
mapTypeControl : true,
streetViewControl : true,
mapTypeControlOptions : {
style : google.maps.MapTypeControlStyle.DEFAULT,
position : google.maps.ControlPosition.TOP_LEFT
}
};
map = new google.maps.Map(document.getElementById("map_canvas"), options);
$("#map_canvas").css("border", "3px solid black");
infoWindow = new google.maps.InfoWindow({});
var c = {
strokeColor: "#ff0000",
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: "#b0c4de",
fillOpacity: 0.50,
map: map,
center: cityCenterLatLng,
radius: 1000,
editable:true
};
circle = new google.maps.Circle(c);
google.maps.event.addListener(circle, 'distance_changed', function()
{
alert('Circle moved');
//displayInfo(circle);
});
google.maps.event.addListener(circle, 'position_changed', function()
{
alert('dictance changed');
//displayInfo(circle);
});
}
function displayInfo(widget)
{
var info = document.getElementById('info');
info.innerHTML = 'Circle Center:' + circle.get('position') + ',Circle distance: ' +
circle.get('distance');
}
回答by Tina CG Hoehr
Change distance_changed
to center_changed
for dragged movement, and use radius_changed
to detect resizing. Other events are in the docs.
更改distance_changed
到center_changed
了拖运动,并使用radius_changed
检测调整。其他事件在文档中。
https://developers.google.com/maps/documentation/javascript/reference#Circle
https://developers.google.com/maps/documentation/javascript/reference#Circle
scroll down to 'Events'.
向下滚动到“事件”。
For the new values, inside the listener functions, write alert(circle.getCenter());
or circle.getRadius()
对于新值,在侦听器函数中,写入alert(circle.getCenter());
或circle.getRadius()