javascript 如何使用 Google Maps API 跟踪多边形的事件变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15114980/
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
How to track event changes to a polygon with Google Maps API
提问by Jonny Haynes
I'm currently building a 'search an area' feature for a site we're working on. A bit like the one on Rightmove.
我目前正在为我们正在开发的网站构建“搜索区域”功能。有点像Rightmove上的那个。
I've got everything up and running except the ability to track event changes to a polygon (the setting of new points and altering existing ones). I need to be able to post the coordinates to the form for submission.
除了能够跟踪多边形的事件更改(设置新点和更改现有点)之外,我已经启动并运行了所有内容。我需要能够将坐标发布到表单中以供提交。
I've tried the Google Code docs for editing events. And every time I try it out, I either get a message about 'set_at' not being possible or my object not being defined.
我已经尝试过使用 Google Code 文档来编辑事件。每次我尝试时,我都会收到一条关于“set_at”不可能的消息,或者我的对象没有被定义。
I suppose the bit I know is wrong is thePolygon
variable not being passed through to the new google.maps.event.addListener(thePolygon, 'set_at', function() {
// grab paths for infoWindow
grabPaths(thePath);
});
我想我知道的错误是thePolygon
变量没有传递给新的google.maps.event.addListener(thePolygon, 'set_at', function() {
// grab paths for infoWindow
grabPaths(thePath);
});
But I don't know why. It's a global variable. Or is not?
但我不知道为什么。这是一个全局变量。或者不是?
So, the question is, how can I track the changes to the polygon to pass the updated coordinates through to my form?
所以,问题是,如何跟踪多边形的变化以将更新的坐标传递到我的表单?
All help is greatly appreciated.
非常感谢所有帮助。
Here is the code I currently have:
这是我目前拥有的代码:
var mapOptions = {
// options
};
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: false,
polygonOptions: {
// drawing options
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
// complete functions
});
google.maps.event.addListener(thePolygon, 'set_at', function() {
// complete functions
});
google.maps.event.addListener(thePolygon, 'insert_at', function() {
// complete functions
});
回答by Dr.Molle
these events are defined for MVCArray, a polyline is not a MVCArray. You must observe the events for the path
of the polyline(which is a MVCArray) instead. Furthermore you can't add a listener to an object that isn't created yet. The polygon is not available before polygoncomplete
, so you must add the listeners inside the polygoncomplete-callback:
这些事件是为 MVCArray 定义的,折线不是 MVCArray。您必须path
改为观察折线(这是一个 MVCArray)的事件。此外,您无法向尚未创建的对象添加侦听器。多边形之前不可用polygoncomplete
,因此您必须在多边形完成回调中添加侦听器:
google.maps.event.addListener(drawingManager,'polygoncomplete',function(polygon) {
// complete functions
google.maps.event.addListener(thePath, 'set_at', function() {
// complete functions
});
google.maps.event.addListener(thePath, 'insert_at', function() {
// complete functions
});
});