我正在尝试简化在 Google 地图(API v3 Javascript)上绘制的复杂多边形。我的目标是接收一组简单的多边形。问题是我画的有些图形可以分析,有些则不能。
例如:
1) 图 8-vertical - 对于带坐标的多边形 (var areaPolygon):
{42.15093256154524, 24.746017456054687}
{42.149087174176515, 24.740352630615234}
{42.14049586965896, 24.754257202148437}
{42.1333673840616, 24.747648239135742}
{42.13795007405907, 24.73846435546875}
{42.145841707270215, 24.74867820739746}
{42.15093256154524, 24.749107360839843}
{42.15093256154524, 24.746017456054687}
它工作正常 - SimplifyPolygon 返回 2 个简单的多边形
2)Figure 8-horizontal - 对于带坐标的多边形(var areaPolygon):
{42.145523515284395, 24.744129180908203}
{42.15016895950386, 24.749279022216797}
{42.14603262169405, 24.7646427154541}
{42.14978715502878, 24.769277572631836}
{42.15055076167604, 24.758892059326172}
{42.14393253136682, 24.75193977355957}
{42.141705086714666, 24.747648239135742}
{42.14348704870535, 24.74472999572754}
{42.145523515284395, 24.744129180908203}
- 它不起作用 - SimplifyPolygon 返回相同的复杂多边形
这是我的代码:
function analyzePolygon(areaPolygon) {
//areaPolygon is google.maps.Polygon
var subj_polygon = new ClipperLib.Polygon();
var result_polygons = new ClipperLib.Polygons();
//LatLng coordinates to integer coordinates
for(var i = 0; i < areaPolygon.getPath().getArray().length ; i++) {
subj_polygon.push(new ClipperLib.IntPoint(Math.round(areaPolygon.getPath().getArray()[i].lat()*100000000000000), Math.round(areaPolygon.getPath().getArray()[i].lng()*100000000000000)));
}
var cpr = new ClipperLib.Clipper();
result_polygons = cpr.SimplifyPolygon(subj_polygon, ClipperLib.PolyFillType.pftNonZero);
//for each simple polygon - make a request and populate markers of the objects which are in these bounds
for(var j=0; j < result_polygons.length; j++) {
var rpStringify = JSON.stringify(eval(result_polygons[j]));
var rpJSON = JSON.parse(rpStringify);
var arrayPolygon = [];
for (var i = 0; i < rpJSON.length; i++) {
var tempObject = JSON.stringify(eval(rpJSON[i]));
var objectJSON = JSON.parse(tempObject);
var tempArr = [2];
tempArr[0] = objectJSON.X/100000000000000;
tempArr[1] = objectJSON.Y/100000000000000;
arrayPolygon[i]= tempArr;
}
loadPinsPolygon(arrayPolygon);
}