一般来说,您需要将google.maps.Polygon对象转换为jsts.geom.Geometry对象,然后运行union操作。如果您想在Google Maps Javascript API v3地图上显示结果,则需要将返回的jsts.geom.Geometry对象转换回google.maps.Polygon。这段代码(来自
this question)将路径转换为jstsPolygon:
var coordinates = googleMaps2JTS(googlePolygonPath);
var geometryFactory = new jsts.geom.GeometryFactory();
var shell = geometryFactory.createLinearRing(coordinates);
var jstsPolygon = geometryFactory.createPolygon(shell);
类似这样:
var geometryFactory = new jsts.geom.GeometryFactory();
var trito = bermudaTriangle.getPath();
var tritoCoor = googleMaps2JTS(trito);
var shell = geometryFactory.createLinearRing(tritoCoor);
var trito2 = secondBermuda.getPath();
var tritoCoor2 = googleMaps2JTS(trito2);
var shell2 = geometryFactory.createLinearRing(tritoCoor2);
var jstsPolygon = geometryFactory.createPolygon(shell);
var jstsPolygon2 = geometryFactory.createPolygon(shell2);
var intersection = jstsPolygon.intersection(jstsPolygon2);
jsfiddle
如果要在结果上使用computeArea方法,则需要将其转换回google.maps.LatLng对象的数组或MVCArray对象(computeArea(path:Array.|MVCArray., radius?:number))
或者您可以使用[jsts.geom.Polygon.getArea](http://bjornharrtell.github.io/jsts/doc/api/symbols/jsts.geom.Polygon.html#getArea) 方法。
working fiddle,使用jsts方法计算三角形、并集和交集的面积。
将结果转换回google.maps.LatLng和google.maps.Polygon对象的代码:
var jsts2googleMaps = function (geometry) {
var coordArray = geometry.getCoordinates();
GMcoords = [];
for (var i = 0; i < coordArray.length; i++) {
GMcoords.push(new google.maps.LatLng(coordArray[i].x, coordArray[i].y));
}
return GMcoords;
}
var intersectionGMArray = jsts2googleMaps(intersection);
然后获取面积:
var intersectionGMarea = google.maps.geometry.spherical.computeArea(intersectionGMArray)
工作的代码片段