From 098f742c77bd7d787f03f07fdf6612792ce34489 Mon Sep 17 00:00:00 2001 From: mdartic Date: Mon, 19 Sep 2016 11:55:58 +0200 Subject: [PATCH 1/2] Adding a test to check latLngs length --- spec/test.closest.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/test.closest.js b/spec/test.closest.js index d9cd149..8dd4ade 100644 --- a/spec/test.closest.js +++ b/spec/test.closest.js @@ -70,6 +70,33 @@ describe('Closest on path with precision', function() { done(); }); + it('It should not alterate the latLngs of a polygon', function(done) { + var polygon = L.polygon([[0, 0], [10, 10], [0, 10]])/*.addTo(map)*/, + ll = [0, 5], + marker = L.marker(ll), + latlngs; + + latlngs = polygon.getLatLngs(); + if (L.Polyline._flat(latlngs)) { + assert.equal(latlngs.length, 3) + } else { + assert.equal(latlngs.length, 1) + assert.equal(latlngs[0].length, 3) + } + + closest = L.GeometryUtil.closest(map, polygon, ll); + + latlngs = polygon.getLatLngs(); + if (L.Polyline._flat(latlngs)) { + assert.equal(latlngs.length, 3) + } else { + assert.equal(latlngs.length, 1) + assert.equal(latlngs[0].length, 3) + } + + done(); + }); + it('It should return null if layer param is not instance of Array|L.Polygon|L.Polyline (Leaflet 0.7.7 only)', function(done) { var campus = { "type": "Feature", From e2e9c8f35104d6a89a3afbf61b3d2170251c2f6b Mon Sep 17 00:00:00 2001 From: mdartic Date: Mon, 19 Sep 2016 11:56:28 +0200 Subject: [PATCH 2/2] Fixing latLngs length test --- src/leaflet.geometryutil.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/leaflet.geometryutil.js b/src/leaflet.geometryutil.js index 1cbc02e..535a206 100644 --- a/src/leaflet.geometryutil.js +++ b/src/leaflet.geometryutil.js @@ -190,7 +190,9 @@ L.GeometryUtil = L.extend(L.GeometryUtil || {}, { } } return result; - } else if (layer[0] instanceof L.LatLng || typeof layer[0][0] === 'number') { // we could have a latlng as [x,y] with x & y numbers + } else if (layer[0] instanceof L.LatLng + || typeof layer[0][0] === 'number' + || typeof layer[0].lat === 'number') { // we could have a latlng as [x,y] with x & y numbers or {lat, lng} layer = L.polyline(layer); } else { return result; @@ -202,7 +204,8 @@ L.GeometryUtil = L.extend(L.GeometryUtil || {}, { if (! ( layer instanceof L.Polyline ) ) return result; - latlngs = layer.getLatLngs().slice(0); + // deep copy of latlngs + latlngs = JSON.parse(JSON.stringify(layer.getLatLngs().slice(0))); // add the last segment for L.Polygon if (layer instanceof L.Polygon) {