如何使用JSTS Library计算Google Maps API中的交叉区域?

xmu*_*mux 4 javascript google-maps google-maps-api-3

我试图计算两个三角形之间的交叉区域.我发现JSTS Topology Suite有一个Geometry类,它有一个方法intersection().我在JSFiddle和我的本地计算机上尝试过,但我得到了一个Uncaught TypeError: undefined is not a function.JSTS 还有一个例子.在这里你可以看到代码.我的代码在JSFiddle中看起来是一样的:

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

还有我应该做的转换吗?

geo*_*zip 7

一般的答案是你需要将google.maps.Polygon对象转换为jsts.geom.Geometry对象,然后运行union.如果要在Google Maps Javascript API v3地图上显示结果,则需要将返回的jsts.geom.Geometry对象转换回google.maps.Polygon.

此代码(来自此问题)将路径转换为jstsPolygon:

var coordinates = googleMaps2JTS(googlePolygonPath);
var geometryFactory = new jsts.geom.GeometryFactory();
var shell = geometryFactory.createLinearRing(coordinates);
var jstsPolygon = geometryFactory.createPolygon(shell);
Run Code Online (Sandbox Code Playgroud)

像这样的东西:

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);
Run Code Online (Sandbox Code Playgroud)

的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)方法.

工作拨弄用的三角形,工会面积以及使用该方法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);
Run Code Online (Sandbox Code Playgroud)

然后到达该区域:

var intersectionGMarea = google.maps.geometry.spherical.computeArea(intersectionGMArray);
Run Code Online (Sandbox Code Playgroud)

工作小提琴