2014-09-25 38 views
3

我正在計算兩個三角形之間的交點的面積。 我發現JSTS Topology Suite有一個Geometry class其中一種方法intersection()。 我試過JSFiddle和我的本地計算機,但我得到了Uncaught TypeError: undefined is not a function。 JSTS還有一個example。在這裏你可以看到代碼。 我的代碼看起來的jsfiddle相同:如何使用JSTS Library計算Google Maps API中的交叉區域?

var union = triangleCoords.union(secondTriangleCoords); 
var intersection = triangleCoords.intersection(secondTriangleCoords); 
console.log('Intersection ' + intersection); 
google.maps.geometry.spherical.computeArea(intersection); 

有,我應該也做了轉換?

+0

我得到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

+1

jsts庫不會將聯合方法添加到Google Maps Polygon類。 – geocodezip 2014-09-25 13:04:12

+0

這個問題的答案[Google Maps Polygons自相交檢測](http://stackoverflow.com/questions/25017463/google-maps-polygons-self-intersecting-detection/25019306#25019306)可能是相關的。 – geocodezip 2014-09-25 13:06:11

回答

6

一般的答案是您需要將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

+1

發現問題,我創建了JSTS coordinates數組,但沒有將它傳遞給LinearRing構造函數,JSTS庫不滿意它所期望的jsts.geom.Coordinate對象的google.maps.LatLng對象... – geocodezip 2014-09-26 03:23:19

+0

添加了用於將生成的jsts多邊形轉換回來的代碼google.maps對象。 – geocodezip 2014-09-26 04:49:32

+0

非常感謝!這很棒!但是我仍然沒有從JSTS獲得[Area Method](http://bjornharrtell.github.io/jsts/doc/api/symbols/jsts.geom.Polygon.html#getArea),它返回什麼?在文檔中只提到'返回這個Polygon的區域'。 你可以請合併[與這個問題](http://stackoverflow.com/questions/24504068/how-to-show-the-intersecting-area-of-two-polygons-on-google-maps),我認爲他們是相同的 謝謝 – xmux 2014-09-26 07:46:17

相關問題