npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

geo-search-helper

v0.2.4

Published

Helper functions for geographical search within search-index.

Downloads

12

Readme

geo-search-helper

Helper functions for geographical search, displaying, filtering and sorting.

NPM version NPM downloads jsDelivr downloads Build Status JavaScript Style Guide MIT License

With this you'll be able to sort search results on shortest to longest distance from your position or some other chosen position. And show i.e. the 10 closest search results or all the search results within a given distance from your position or some other chosen position. Will be possible to do sorting on numbers in version 3 of search-index.

Getting started

UMD script tag

<script src="geo-search-helper-umd.js"></script>
<!-- gsh.mapBoundsPosKm, gsh.mapBoundsPoints, gsh.getDistanceFromLatLonInKm available -->

Through JsDelivr

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/geo-search-helper.umd.min.js"></script>
<!-- gsh.mapBoundsPosKm, gsh.mapBoundsPoints, gsh.getDistanceFromLatLonInKm available -->

ESM script tag

<script type="module">
   import { mapBoundsPosKm, mapBoundsPoints, getDistanceFromLatLonInKm } from 'geo-search-helper.esm.mjs'
</script>

Through JsDelivr

<script type="module">
   import { mapBoundsPosKm, mapBoundsPoints, getDistanceFromLatLonInKm } from 'https://cdn.jsdelivr.net/npm/geo-search-helper/dist/geo-search-helper.esm.min.mjs'
</script>

CJS

const { getDistanceFromLatLonInKm, mapBoundsPoints, mapBoundsPosKm } = require('geo-search-helper')

Ways of using the library

First you get a search result, calculate distance from point of interest and sort it on smallest to highest distance.

Then there is two ways of using the library. Either your use case is to show search results within a given distance, or show [n] search results.

For search results within a given distance, you cut of the results biggere than that given distance and find map bounds with mapBoundsPosKm() to be able to show the search results on a map.

Cutting of search results after a given distance.

For showing the first [n] search results, you use mapBoundsPoints() to be able to show the search results on a map.

Cutting of search results after a given number of results.

API

getDistanceFromLatLonInKm({fromPointObj}, {toPointObj})

Calculating distances from point of interest to other geographical points.

Returns shortest distance over the earth’s surface – using the ‘Haversine’ formula. Result in kilometers. To i.e. sort a search result from a point on the map.

mapBoundsPosKm({fromPointObj}, radius)

Returns object with coordinates of map boundaries based on given position and radius from that position.

Input

Input to the function: Point of interest and radius.

Output

Output from the function: Upper, left coordinate and bottom right coordinate for a map view.

mapBoundsPoints([poinstArray], ['keyLatValue'], ['keyLonValue'])

Returns object with coordinates of map boundaries based on area covered by an array of geographical points. Needs two or more points to work.

For latitude and longitude in nested objects like:

const data = [
  {
    id: '14272199162',
    geo: {
      latitude: 59.907427,
      longitude: 10.785616
    },
    url: 'https://live.staticflickr.com/5513/14272199162_e7547e4394_b.jpg',
    height: 1024,
    width: 1024,
    urlphotopage: 'https://flickr.com/photos/breial/14272199162'
  }
]

You can access the latitude by calling ['geo', 'latitude'] fro latitude and ['geo', 'longitude'] for longitude.

Input

Input to the function: Map coordinates from search result in addition to your point of interest, if you choose.

Output

Output from the function: Upper, left coordinate and bottom right coordinate for a map view.

What's implemented

Browser focus with ESM and UMD, three functions:

  • [x] Get distance from one point on a map to another point (either currentPosition or something else). Points described with lat/lon
  • [x] Get map boundaries (lat/lon for SW and NE) based on a location + a radius in km from that point (either through getCurrentPosition or something else).
  • [x] Get map boundaries (lat/lon for SW and NE) based on a lot of locations in a map, sorting them to find the most northern, eastern, southern and western point. This way you can have a map boundary for displaying i.e. 10 search results.

getCurrentPosition

The fromPointObj can be your current position or something else, like one of the search results.

Example

// Position options
const getPositionOpt = {
   enableHighAccuracy: true,
   timeout: 5000,
   maximumAge: 0
}

// Position promise
function getPosition() {
   return new Promise((res, rej) => {
      navigator.geolocation.getCurrentPosition(res, rej, getPositionOpt)
   })
}

// Tying all the position stuff together
function getPositionCallback() {
   getPosition()
   // format position object
   .then((pos) => {
      const crd = pos.coords;
      const position = { lat: crd.latitude, lon: crd.longitude, acc: crd.accuracy }          
      return position
   })
   // Do your stuff here
   .then((position) => {
      populateHTML(position)
      console.log(position)
   })
   // Handle errors
   .catch((err) => {
      console.log(err)
      const error = { errortype: err.code, errormessage: err.message }
      populateHTML(error)
      return error
   })
}

function showMap(position) {
   const map = L.map('map',
   {
      center: [position.lat, position.lon],
      zoom: 10
   }
   );
   L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
}

// Dummy HTML populate
function populateHTML(msg) {
   const node = document.createElement('span').innerText = JSON.stringify(msg, 2, ' ')
   document.getElementById('pos').replaceChildren(node)
}

// Fire up the position magic
getPositionCallback()

Issue when map shown is not square

When map shown is not square, the square mapview will be within the boundaries of the horisontal or vertical map. Not a big problem, but okay to know about.

When map shown is not square, the square mapview will be within the boundaries of the horisontal or vertical map.