【发布时间】:2017-01-02 22:50:23
【问题描述】:
在使用 Google 地图绘图模式时,我需要跟踪绘制形状的坐标。绘制形状(例如多边形)后,我可以添加事件侦听器以在click 和dragend 事件中记录给定的 ID 和坐标,但在编辑形状时它们将不起作用(例如 insert_at、@987654325 @,set_at)。
var shapeID = 1;
google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
drawingManager.setDrawingMode(null);
polygon.setOptions({ id: shapeID, editable:true, draggable:true });
google.maps.event.addListener(polygon, 'click', function() {
console.log(this.id+' '+this.getPath().getArray().toString());
});
google.maps.event.addListener(polygon, 'dragend', function() {
console.log(this.id+' '+this.getPath().getArray().toString());
});
google.maps.event.addListener(polygon, 'insert_at', function() {
console.log(this.id+' '+this.getPath().getArray().toString());
});
google.maps.event.addListener(polygon, 'remove_at', function() {
console.log(this.id+' '+this.getPath().getArray().toString());
});
google.maps.event.addListener(polygon, 'set_at', function() {
console.log(this.id+' '+this.getPath().getArray().toString());
});
shapeID++;
});
设置多边形选项可以正常工作;如果您单击多边形,它们的正确 shapeID 会记录在控制台中。
我的问题是我想添加这样的事件:
google.maps.event.addListener(polygon.getPath(), "insert_at", getPath);
google.maps.event.addListener(polygon.getPath(), "remove_at", getPath);
google.maps.event.addListener(polygon.getPath(), "set_at", getPath);
function getPath() {
var path = polygon.getPath();
var len = path.getLength();
var coordStr = 'id: '+polygon.id+'\n';
for (var i=0; i<len; i++) {
coordStr += path.getAt(i).toUrlValue(6)+"\n";
}
console.log(coordStr);
}
但我无法通过分配给它们的 shapeID 访问这些形状。谷歌地图不允许我在字符串中分配多边形 ID:
google.maps.event.addListener(POLYGON_ID.getPath(), "insert_at", getPath);
我收到一条错误消息,提示“a is not defined”。
【问题讨论】:
-
这不是重复的 - 您提供的链接在点击时提供了更新的坐标,而不是在拖动点时。