javascript - 如何从 Google Map JavaScript 中删除多边形的单边
问题描述
window.google.maps.event.addListener(
this.poly, //currently selected polygon
"mouseup",
(event) => {
if (this.insidePolygon(event.latLng)) {
const path = this.poly.getPaths().getAt(event.path);
if (event?.vertex >= 0) { //returns true if vertex is being dragged
path.removeAt(event.vertex); // removes the current vertex
} else if (event?.edge >= 0) { //returns true if edge is being dragged
path.removeAt(event.edge); // does not remove the current edge
}
} else {
this.calcAreaAndLatlng();
}
}
);
我在谷歌地图中有多个多边形。我正在尝试使用mouseup事件来拖动和编辑多边形。我不希望我的多边形重叠。这就是为什么我使用insidePolygon
如果当前多边形与任何其他多边形重叠则返回 true 的函数。因此,当函数返回 true 时,我使用path.removeAt(event.vertex)
删除试图重叠的顶点。但是当我尝试拖动edge而不是vertex时,问题就开始了。然后path.removeAt(event.edge)
即使两者都不起作用event.vertex
并event.edge
分别返回顶点和边的索引。有没有办法像我删除顶点一样删除边缘?
解决方案
这对我有用,但是如果没有看到您向我们展示内容this.polygon
和this.insidePolygon
外观的完整代码,就很难诊断。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style type="text/css">
html,body { height: 100%; margin: 0; padding: 0 }
#map { width:800px; height:640px }
</style>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
var MapClass = class {
constructor() {
this.map = new google.maps.Map(document.getElementById("map"), {
zoom: 15,
center: {lat: 51.476706, lng: 0},
mapTypeId: google.maps.MapTypeId.ROADMAP
});
}
drawPentagon() {
var coords = [
new google.maps.LatLng(51.474821, -0.001935),
new google.maps.LatLng(51.474647, 0.003966),
new google.maps.LatLng(51.477708, 0.004073),
new google.maps.LatLng(51.479753, 0.000468),
new google.maps.LatLng(51.477654, -0.002192)
];
this.poly = new google.maps.Polygon({
paths: coords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 10,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: this.map,
draggable: true,
editable: true
});
}
addEventListeners() {
window.google.maps.event.addListener(
this.poly,
"mouseup",
(event) => {
if (this.insidePolygon(event.latLng)) {
const path = this.poly.getPaths().getAt(event.path);
if (event?.vertex >= 0) { //returns true if vertex is being dragged
path.removeAt(event.vertex); // removes the current vertex
} else if (event?.edge >= 0) { //returns true if edge is being dragged
path.removeAt(event.edge); // does not remove the current edge
}
} else {
this.calcAreaAndLatlng();
}
}
);
}
insidePolygon(latLng) {
console.log('insidePolygon', latLng);
return true;
}
calcAreaAndLatlng() {
console.log('calcAreaAndLatlng');
}
};
function initialize() {
var map = new MapClass();
map.drawPentagon();
map.addEventListeners();
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
推荐阅读
- python-3.x - 当我们实例化一个对象时,python中的类变量是否表现为实例变量?
- javascript - 无法从ul中删除多个li
- python - 具有 % 值的 ConfigParser 选项
- windows - 将桌面文件夹重新定位到 OneDrive 后,%USERPROFILE%/Desktop 不再有效
- sql - SQL 错误 [22007]:日期、时间或时间戳值无效。SQLCODE=-180,SQLSTATE=22007,驱动程序=4.9.78
- wordpress - 只显示给定单词的简码一次
- php - 在中间件中获取 laravel 路由 {slug}
- python - 在有向无环图 Python 中面临句法问题
- sql - 在 Oracle 中依靠 UNION
- html - 使标签的整个宽度可点击和响应