nearestjs

Utility library for calculating the nearest set of things to a target thing, geospatially speaking

Usage no npm install needed!

<script type="module">
  import nearestjs from 'https://cdn.skypack.dev/nearestjs';
</script>

README

Nearest.js

Nearest.js is a small utility library for calculating the nearest set of things to a target thing, geospatially speaking. In JavaScript.

Demo

For example, a list of restaurant reviews near to where you work,

API

Include in your page,

<script src="nearest.js"></script>

... and the Nearest.find function will return the items in that list sorted approximately in order of the distance from a given coordinate.

results = new Nearest().find(dataSource, {
   coords: [51.5349, -0.1219],
   limit: 10, 
   within: 3
})

The parameters in full,

  • dataSource - a data structure as per the format outline below.
  • coords - the lat/lon pair of the central location you want the search to start from.
  • limit - limit the number of results returned.
  • within - limit results to within n kilometers.

Data

Nearest.js requires a datasource containing a list of objects, each containing a lat and lon property.

  [
    {
        "path": "lifeandstyle/2012/jan/08/jay-rayner-restaurant-review-create",
        "restaurant": "Create",
        "reviewer": "John Lanchester",
        "lat": "53.797331",
        "lon": "-1.549829"
    },
    {
        "path": "lifeandstyle/2012/jan/06/granger-and-co-restaurant-review",
        "reviewer": "John Lanchester",
        "lat": "51.514502",
        "lon": "-0.198357",
        "links": [
            "foo": "http://example.com/foo",
            "bar": "http://example.com/bar"
        ]
    }
    ...
  ]