一般的答案是您需要將google.maps.Polygon對象轉換爲jsts.geom.Geometry對象,然後運行聯合。如果要在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方法的結果,雖然,您需要將其轉換回數組或MVCrray的google.maps.LatLng對象(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);
working fiddle
我得到2你的小提琴JavaScript錯誤'未捕獲的ReferenceError:JavaScript未定義'和'未捕獲的TypeError:未定義不是函數'。我會說你缺少jsts所需的外部JavaScript庫。也許[這一個](http://bjornharrtell.github.io/jsts/lib/javascript.util.js)。更改包含的順序[修復](http://jsfiddle.net/cbs2h59e/7/) – geocodezip 2014-09-25 12:57:57
jsts庫不會將聯合方法添加到Google Maps Polygon類。 – geocodezip 2014-09-25 13:04:12
這個問題的答案[Google Maps Polygons自相交檢測](http://stackoverflow.com/questions/25017463/google-maps-polygons-self-intersecting-detection/25019306#25019306)可能是相關的。 – geocodezip 2014-09-25 13:06:11