我有一个谷歌脚本,可以创建形状和删除形状,但是对于保存形状的内容了解不多。
我查阅了互联网,发现可以通过在overlaycomplete内使用getpaths()访问路径坐标,并将这些坐标推入一个数组中以收集所有形状。然而,如果用户删除或更新形状会怎样呢?
我的问题是我不知道如何识别已删除的形状,以便相应地更新形状集合数组。
第二个问题是我不知道如何确定用户是否更新了形状以及哪个形状被更新。因此,无法更新形状集合数组。
当单击“保存”按钮时,我需要一个解决方案,可以获取地图上存在的所有形状的坐标。因此,我不必担心哪些形状已被更新或删除。我的当前代码如下:
我查阅了互联网,发现可以通过在overlaycomplete内使用getpaths()访问路径坐标,并将这些坐标推入一个数组中以收集所有形状。然而,如果用户删除或更新形状会怎样呢?
我的问题是我不知道如何识别已删除的形状,以便相应地更新形状集合数组。
第二个问题是我不知道如何确定用户是否更新了形状以及哪个形状被更新。因此,无法更新形状集合数组。
当单击“保存”按钮时,我需要一个解决方案,可以获取地图上存在的所有形状的坐标。因此,我不必担心哪些形状已被更新或删除。我的当前代码如下:
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=drawing,geometry,places"></script>
<style>
#map,html,body{
height:100%;
margin:0;
padding:0;
}
#delete-button, #save-button {
margin-top: 5px;
position:absolute;
z-index:999;
right:6%;
top:3%;
}
#save-button{
right:1%;
}
</style>
</head>
<body>
<div id="map"></div>
<button id="delete-button" class="btn">Delete seleted shape</button>
<button id="save-button" class="btn" onclick="save();">Save all</button>
<script>
var drawingManager;
var selectedShape;
var shapeCollection = [];
function save(){
console.log(shapeCollection);
}
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
shape.setEditable(true);
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
}
function initialize() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom:10,
center: new google.maps.LatLng(22.344, 114.048),
mapTypeId: google.maps.MapTypeId.ROADMAP,
});
var polyOptions = {
strokeWeight: 3,
fillOpacity: 0.2,
editable: true,
draggable: true
};
// Creates a drawing manager attached to the map that allows the user to draw
// markers, lines, and shapes.
drawingManager = new google.maps.drawing.DrawingManager({
drawingControl: true,
drawingControlOptions: {
drawingModes: [google.maps.drawing.OverlayType.POLYGON]
},
drawingMode: google.maps.drawing.OverlayType.POLYGON,
polygonOptions: polyOptions,
map: map
});
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
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);
});
//polygonPoints will be the array and index of the array will be the order
// Clear the current selection when the drawing mode is changed, or when the
// map is clicked.
google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
google.maps.event.addListener(map, 'click', clearSelection);
google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</body>
</html>
save
方法返回的形状),您还必须将它们添加到集合中。我已经添加了一个示例。 - Dr.Molle