From 20427048c72998e610fb7d331ba5ddc0f15284d1 Mon Sep 17 00:00:00 2001 From: Bruce Date: Tue, 8 May 2018 11:19:42 +1000 Subject: [PATCH] Fix tests failure and the check logic --- tests/wicket-gmap3-spec.js | 23 +++++++++++++------- wicket-gmap3.js | 43 ++++++++------------------------------ 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/tests/wicket-gmap3-spec.js b/tests/wicket-gmap3-spec.js index 7e3ec6d..4d51d5f 100644 --- a/tests/wicket-gmap3-spec.js +++ b/tests/wicket-gmap3-spec.js @@ -116,7 +116,8 @@ describe('Standard WKT Test Cases: ', function() { new google.maps.LatLng(10, 30), new google.maps.LatLng(40, 40), new google.maps.LatLng(40, 20), - new google.maps.LatLng(20, 10) + new google.maps.LatLng(20, 10), + new google.maps.LatLng(10, 30) ] ] }), @@ -175,12 +176,14 @@ describe('Standard WKT Test Cases: ', function() { new google.maps.LatLng(10, 35), new google.maps.LatLng(45, 45), new google.maps.LatLng(40, 15), - new google.maps.LatLng(20, 10) + new google.maps.LatLng(20, 10), + new google.maps.LatLng(10, 35) ], [ // Order in inner rings is reversed new google.maps.LatLng(30, 21), new google.maps.LatLng(35, 35), - new google.maps.LatLng(20, 30) + new google.maps.LatLng(20, 30), + new google.maps.LatLng(30, 21) ] ] }), @@ -360,7 +363,8 @@ describe('Standard WKT Test Cases: ', function() { [ new google.maps.LatLng(20, 30), new google.maps.LatLng(40, 45), - new google.maps.LatLng(40, 10) + new google.maps.LatLng(40, 10), + new google.maps.LatLng(20, 30) ] ] }), @@ -371,7 +375,8 @@ describe('Standard WKT Test Cases: ', function() { new google.maps.LatLng(5, 15), new google.maps.LatLng(10, 40), new google.maps.LatLng(20, 10), - new google.maps.LatLng(10, 5) + new google.maps.LatLng(10, 5), + new google.maps.LatLng(5, 15) ] ] }) @@ -461,7 +466,8 @@ describe('Standard WKT Test Cases: ', function() { [ new google.maps.LatLng(40, 40), new google.maps.LatLng(45, 20), - new google.maps.LatLng(30, 45) + new google.maps.LatLng(30, 45), + new google.maps.LatLng(40, 40) ] ] }), @@ -473,13 +479,14 @@ describe('Standard WKT Test Cases: ', function() { new google.maps.LatLng(30, 10), new google.maps.LatLng(10, 10), new google.maps.LatLng(5, 30), - new google.maps.LatLng(20, 45) + new google.maps.LatLng(20, 45), + new google.maps.LatLng(35, 20) ], [ new google.maps.LatLng(20, 30), new google.maps.LatLng(15, 20), new google.maps.LatLng(25, 20), - + new google.maps.LatLng(20, 30) ] ] }) diff --git a/wicket-gmap3.js b/wicket-gmap3.js index a087ea2..007aaee 100644 --- a/wicket-gmap3.js +++ b/wicket-gmap3.js @@ -156,7 +156,7 @@ * @return {google.maps.Polygon} */ polygon: function (config, component) { - var j, k, c, rings, verts; + var j, k, c, rings, verts, outerClockwise; var polygonIsClockwise = function (coords) { var area = 0, @@ -188,10 +188,13 @@ } // eo for each vertex - if (j !== 0) { - // Orient inner rings correctly - if (polygonIsClockwise(c[j]) && this.type == 'polygon') { - verts.reverse(); + if (j === 0) { + outerClockwise = polygonIsClockwise(c[j]); + } else { + // Note that the points forming the inner path are wound in the + // opposite direction to those in the outer path, to form the hole + if (polygonIsClockwise(c[j]) === outerClockwise && this.type == 'polygon') { + verts.reverse(); } } @@ -336,19 +339,8 @@ return false; } - if (l === 2) { - // If clockwise*clockwise or counter*counter, i.e. - // (-1)*(-1) or (1)*(1), then result would be positive - if (sign(obj.getPaths().getAt(0)) * sign(obj.getPaths().getAt(1)) < 0) { - return false; // Most likely single polygon with 1 hole - } - - return true; - } - - // Must be longer than 3 polygons at this point... areas = obj.getPaths().getArray().map(function (k) { - return sign(k) / Math.abs(sign(k)); // Unit normalization (outputs 1 or -1) + return sign(k) > 0 ? 1 : -1; // Unit normalization (outputs 1 or -1) }); // If two clockwise or two counter-clockwise rings are found @@ -359,7 +351,6 @@ } return false; - }()); } @@ -380,22 +371,6 @@ x: tmp.getAt(0).lng(), y: tmp.getAt(0).lat() }); - - if (i % 2 !== 0) { // In inner rings, coordinates are reversed... - verts.reverse(); - } - } - - if (obj.getPaths().length > 1 && i > 0) { - // If this and the last ring have the same signs... - if (sign(obj.getPaths().getAt(i)) > 0 && sign(obj.getPaths().getAt(i - 1)) > 0 || - sign(obj.getPaths().getAt(i)) < 0 && sign(obj.getPaths().getAt(i - 1)) < 0 /*&& !multiFlag*/ ) { - // ...They must both be inner rings (or both be outer rings, in a multipolygon) - verts = [verts]; // Wrap multipolygons once more (collection) - } else { - verts.reverse(); - } - } rings.push(verts);