我在使用 Javascript 和 Google Maps API 进行此操作时遇到问题。
我有一个矩形及其边界(纬度和经度)。 另外,我有一个多边形及其边界(每个点的纬度和经度)。
我需要得到一个新的多边形,第一个矩形没有多边形,所以我需要将多边形减去矩形并得到新多边形的边界。
我该怎么做?
提前致谢!
最佳答案
给你:
// Create a rectangle
var rectangle = new google.maps.Rectangle({
strokeOpacity: 0,
strokeWeight: 0,
fillColor: '#FF0000',
fillOpacity: .6,
bounds: new google.maps.LatLngBounds(
new google.maps.LatLng(33, 3),
new google.maps.LatLng(44, 25)
),
zIndex: 0
});
// Get the rectangle bounds
var rb = rectangle.getBounds();
// Create a triangle
var triangle = new google.maps.Polygon({
strokeOpacity: 0,
strokeWeight: 0,
fillColor: '#00FF00',
fillOpacity: .6,
paths: [
new google.maps.LatLng(39, 4),
new google.maps.LatLng(34, 24),
new google.maps.LatLng(43, 24),
new google.maps.LatLng(39, 4)
],
zIndex: 1
});
// Get rectangle points coordinates (path)
var rectangleCoords = [
new google.maps.LatLng(rb.getNorthEast().lat(), rb.getSouthWest().lng()),
new google.maps.LatLng(rb.getNorthEast().lat(), rb.getNorthEast().lng()),
new google.maps.LatLng(rb.getSouthWest().lat(), rb.getNorthEast().lng()),
new google.maps.LatLng(rb.getSouthWest().lat(), rb.getSouthWest().lng()),
new google.maps.LatLng(rb.getNorthEast().lat(), rb.getSouthWest().lng())
];
// Create polygon from rectangle coords and triangle path
var polygon = new google.maps.Polygon({
paths: [rectangleCoords, triangle.getPath()],
strokeOpacity: 0,
strokeWeight: 0,
fillColor: '#FF0000',
fillOpacity: .6,
map: map
});
注意:一个更简单的解决方案是像三 Angular 形一样创建矩形(多边形而不是矩形),这样您就可以直接调用 getPath()
。
希望这对您有所帮助。下面是一个工作演示。
关于Javascript:用纬度和经度减去多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28233018/