gpt4 book ai didi

javascript - 在 Google Maps API v3 中修改多边形后的事件

转载 作者:行者123 更新时间:2023-12-03 01:56:41 26 4
gpt4 key购买 nike

我制作了一个使用绘图管理器(并实现可选形状)的 map 应用程序。该程序的工作原理如下:当完成绘制多边形后,单击按钮,将路径映射到多边形上。

在此过程之后编辑多边形时,我希望再次调用映射函数。但是我无法让这部分工作:

我尝试使用以下代码,但总是收到错误,因为添加此监听器时尚未选择任何形状。我能做什么?

google.maps.event.addListener(selectedShape, 'set_at', function() {
console.log("test");
});

google.maps.event.addListener(selectedShape, 'insert_at', function() {
console.log("test");
});

重要的代码片段:

function showDrawingManager(){
var managerOptions = {
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [google.maps.drawing.OverlayType.MARKER, google.maps.drawing.OverlayType.POLYLINE, google.maps.drawing.OverlayType.POLYGON]
},
markerOptions: {
editable: true,
icon: '/largeTDGreenIcons/blank.png'
},
polygonOptions: {
fillColor: "#1E90FF",
strokeColor: "#1E90FF",
},
polylineOptions: {
strokeColor: "#FF273A"
}
}

var drawingManager = new google.maps.drawing.DrawingManager(managerOptions);
drawingManager.setMap(map);
return drawingManager;
}

function clearSelection() {
if (selectedShape) {
console.log("clearSelection");

selectedShape.setEditable(false);
selectedShape = null;
numberOfShapes--;
}
}

function setSelection(shape) {

console.log("setSelection");

clearSelection();
selectedShape = shape;
shape.setEditable(true);
numberOfShapes++;
//getInformation(shape);
}

function initialize(){

//....

var drawingManager = showDrawingManager();
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
if (e.type != google.maps.drawing.OverlayType.MARKER) {
// Switch back to non-drawing mode after drawing a shape.
drawingManager.setDrawingMode(null);

// Add an event listener that selects the newly-drawn shape when the user
// mouses down on it.
var newShape = e.overlay;
newShape.type = e.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});

最佳答案

我通过调用 .getPath() 并将监听器放入监听器中来解决这个问题,每次单击形状时都会调用该监听器。我认为 Google API 文档对于如何使用 set_at 不是很清楚,因此它对其他人也可能有用。

// Add an event listener that selects the newly-drawn shape when the user
// mouses down on it.
var newShape = e.overlay;
newShape.type = e.type;
google.maps.event.addListener(newShape, 'click', function() {
google.maps.event.addListener(newShape.getPath(), 'set_at', function() {
console.log("test");
});

google.maps.event.addListener(newShape.getPath(), 'insert_at', function() {
console.log("test");
});
setSelection(newShape);
});

关于javascript - 在 Google Maps API v3 中修改多边形后的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12515748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com