leaflet-geometryutil
Advanced tools
Comparing version
@@ -229,2 +229,41 @@ // Packaging/modules magic dance. | ||
/** | ||
* Returns all layers within a radius of the given position, in an ascending order of distance. | ||
@param {L.Map} map | ||
@param {Array<ILayer>} layers - A list of layers. | ||
@param {L.LatLng} latlng - The position to search. | ||
@param {?Number} [radius=Infinity] - Search radius in pixels | ||
@return {object[]} an array of object including layer within the radius, closest latlng, and distance | ||
*/ | ||
layersWithin: function(map, layers, latlng, radius) { | ||
radius = typeof radius == 'number' ? radius : Infinity; | ||
var results = []; | ||
var ll = null; | ||
var distance = 0; | ||
for (var i = 0, n = layers.length; i < n; i++) { | ||
var layer = layers[i]; | ||
if (typeof layer.getLatLng == 'function') { | ||
ll = layer.getLatLng(); | ||
distance = L.GeometryUtil.distance(map, latlng, ll); | ||
} | ||
else { | ||
ll = L.GeometryUtil.closest(map, layer, latlng); | ||
if (ll) distance = ll.distance; // Can return null if layer has no points. | ||
} | ||
if (ll && distance < radius) { | ||
results.push({layer: layer, latlng: ll, distance: distance}); | ||
} | ||
} | ||
var sortedResults = results.sort(function(a, b) { | ||
return a.distance - b.distance; | ||
}); | ||
return sortedResults; | ||
}, | ||
/** | ||
Returns the closest position from specified {LatLng} among specified layers, | ||
@@ -287,2 +326,5 @@ with a maximum tolerance in pixels, providing snapping behaviour. | ||
// ensure the ratio is between 0 and 1; | ||
ratio = Math.max(Math.min(ratio, 1), 0); | ||
if (ratio === 0) { | ||
@@ -301,5 +343,2 @@ return { | ||
// ensure the ratio is between 0 and 1; | ||
ratio = Math.max(Math.min(ratio, 1), 0); | ||
// project the LatLngs as Points, | ||
@@ -507,3 +546,3 @@ // and compute total planar length of the line at max precision | ||
Returns the bearing in degrees clockwise from north (0 degrees) | ||
from the first L.LatLng to the second, at the first LatLng | ||
from the first L.LatLng to the second, at the first LatLng | ||
@param {L.LatLng} latlng1: origin point of the bearing | ||
@@ -510,0 +549,0 @@ @param {L.LatLng} latlng2: destination point of the bearing |
{ | ||
"name": "leaflet-geometryutil" | ||
, "version": "0.4.0" | ||
, "version": "0.5.0" | ||
, "description": "Leaflet utility functions on geometries" | ||
@@ -5,0 +5,0 @@ , "keywords": ["Leaflet", "GIS"] |
@@ -11,2 +11,15 @@ Leaflet.GeometryUtil | ||
Using Node: | ||
``` | ||
npm install leaflet-geometryutil | ||
``` | ||
Or browser: | ||
``` | ||
<script src="leaflet.geometryutil.js"></script> | ||
``` | ||
Check out [online documentation](http://makinacorpus.github.io/Leaflet.GeometryUtil/). | ||
@@ -34,5 +47,6 @@ | ||
--------- | ||
### master ### | ||
### 0.5.0 ### | ||
* Nothing changed yet. | ||
- Add function `layersWithin()` (#34, thanks @haoliangyu) | ||
* Fix safety check on the ratio value in ``interpolateOnLine()` (#29, thanks @Marcussacapuces91) | ||
@@ -39,0 +53,0 @@ ### 0.4.0 ### |
@@ -200,3 +200,30 @@ var assert = chai.assert; | ||
describe('Layers within a radius of the given location', function() { | ||
it('It should return an empty array if the list is empty', function(done) { | ||
var ll = L.latLng([0, 0]); | ||
var results = L.GeometryUtil.layersWithin(map, [], ll); | ||
assert.equal(0, results.length); | ||
done(); | ||
}); | ||
it('It should return an array containing one layer', function(done) { | ||
var ll = L.latLng([0, 0]); | ||
var layers = [L.marker([2, 2]), L.marker([100, 100])]; | ||
var results = L.GeometryUtil.layersWithin(map, layers, ll, 5); | ||
assert.equal(1, results.length); | ||
assert.deepEqual(results[0], {layer: layers[0], latlng: layers[0].getLatLng(), distance: Math.sqrt(2)}); | ||
done(); | ||
}); | ||
it('It should return an array containing two layers ordered by distance', function(done) { | ||
var ll = L.latLng([0, 0]); | ||
var layers = [L.marker([2, 2]), L.marker([3, 3])]; | ||
var results = L.GeometryUtil.layersWithin(map, layers, ll, 10); | ||
assert.equal(2, results.length); | ||
assert.equal(true, results[0].distance < results[1].distance); | ||
done(); | ||
}); | ||
}); | ||
describe('Closest snap', function() { | ||
@@ -290,2 +317,3 @@ var square, diagonal, d, w, layers; | ||
it('It should be the first vertex if offset is 0', function(done) { | ||
@@ -298,2 +326,9 @@ var interp = L.GeometryUtil.interpolateOnLine(map, [llA, llB], 0); | ||
it('It should be the first vertex if offset is less than 0', function(done) { | ||
var interp = L.GeometryUtil.interpolateOnLine(map, [llA, llB], -10); | ||
assert.latLngEqual(interp.latLng, llA); | ||
assert.equal(interp.predecessor, -1); | ||
done(); | ||
}); | ||
it('It should be the last vertex if offset is 1', function(done) { | ||
@@ -306,2 +341,9 @@ var interp = L.GeometryUtil.interpolateOnLine(map, [llA, llB, llC], 1); | ||
it('It should be the last vertex if offset is more than 1', function(done) { | ||
var interp = L.GeometryUtil.interpolateOnLine(map, [llA, llB, llC], 10); | ||
assert.latLngEqual(interp.latLng, llC); | ||
assert.equal(interp.predecessor, 1); | ||
done(); | ||
}); | ||
it('It should not fail if line has no length', function(done) { | ||
@@ -308,0 +350,0 @@ var interp = L.GeometryUtil.interpolateOnLine(map, [llA, llA, llA], 0.5); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
109
14.74%142384
-43.82%19
-29.63%1538
-22.64%