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

mapwrap

v2.1.0

Published

Simple wrapper around Google Maps API

Downloads

6

Readme

MapWrap :pushpin:

npm version npm bundle size

Changelog - updated for 2.1.0

About

MapWrap is a simple wrapper around the Google Maps API for Geocoding, Directions, and Places (Nearby Search, Place Details). It provides simple methods to access data members in the API response with error checking.

MapWrap also implements a simple LRU cache to speed up requests and decrease the amount of unnecessary calls to the server. It is fully compliant with Google's restriction of not caching data for over 30 days.

Installation

npm install --save mapwrap

Usage

const MapWrap = require("mapwrap");

const mapwrap = MapWrap({
  // DEFAULT_API_KEY is all you need to get started. 
  // All other config properties are optional.
  DEFAULT_API_KEY: "my_google_api_key",
  // Optional keys with priority over default for specific API services.
  useRestrictedKeys: {
    GEOCODING_API_KEY: "geocoding_api_key", 
    DIRECTIONS_API_KEY: "directions_api_key", // these are null by default.
    PLACES_API_KEY: "places_api_key"
  },
  // Set the max age (in ms) of an item in the cache before it is pruned.
  // Default max age is 5 minutes. Min age is 1 minute and max is 30 days.
  cacheMaxItemAges: {
    reverseGeoCache: 5 * 60000,
    geoCache: 20 * 60000,
    directionsCache: 15 * 60000,
    nearbySearchCache: 300 * 60000,
    placeDetailsCache: 300 * 60000,
  },
  // Set the size of your LRU cache 
  // Default cache sizes are 10 items
  cacheMaxSizes: {
    reverseGeoCache: 20, 
    geoCache: 20,
    directionsCache: 15,
    nearbySearchCache: 10,
    placeDetailsCache: 10
  },
  // Enable printing of a message when a cache is accessed. False by default.
  logCache: true,
  // Use a custom logger function to track cache hits. Defaults to `console.log`.
  logger: console.info
});

(async () => {
  try {

    const geoWrapper = await mapwrap.geocode("Staples Center, LA");
    console.log(geoWrapper.getAllAddresses()); // return an array of objects
    console.log(geoWrapper.getTopAddress()); // return first address in its object
    console.log(geoWrapper.getTopAddress(true)); // return first address as a string

    const reverseGeoWrapper = await mapwrap.reverseGeocode(33.651021, -117.841550);
    console.log(reverseGeoWrapper.getAllAddresses());
    console.log(reverseGeoWrapper.getTopAddress());
    console.log(reverseGeoWrapper.getTopAddress(true));

    /*
    Allowed modes: https://developers.google.com/maps/documentation/directions/intro#TravelModes
    */
    const directionsParams = {
      origin: "Anaheim", // required
      destination: "Irvine", // required
      mode: "driving",
      altRoutes: true, // false by default. retrieve alternate routes (up to 3, usually)
      avoidFerries: false,
      avoidHighways: false,
      avoidIndoor: false,
      avoidTolls: false,
      units: "imperial" // or "metric"
    };

    const directionsWrapper = await mapwrap.directions(directionsParams);

    /* 
    The methods getStartAddress, getEndAddress, getRoute, and getRouteSteps
    accepts an optional number to specify which route you want to retrieve data from.
    Useful only when altRoutes is set to true in directionsParams.
    Defaults to 0 if not specified.
    */
    console.log(directionsWrapper.getStartAddress(1));
    console.log(directionsWrapper.getEndAddress()); 
    console.log(directionsWrapper.getRoute(1)); 
    console.log(directionsWrapper.getRoutes());
    console.log(directionsWrapper.getRouteSteps());

    const placeSearchWrapper = await mapwrap.nearbySearchPlaces({
      location: {
        lat: 33.651021, 
        lng: -117.841550
      },
      radius: 30000, // radius in meters to search nearby.
      keyword: "starbucks", // optional
      // units: "imperial" As of 2.1.0, No longer needed.
    });

    console.log(placeSearchWrapper.getResults());
    /*
    What is next page token?
    https://developers.google.com/places/web-service/search#PlaceSearchPaging
    */
    console.log(placeSearchWrapper.getNextPageToken());

    /* 
    commented out as Google Maps API requires a small amount of time before the next page token becomes valid.
    https://stackoverflow.com/questions/21265756/paging-on-google-places-api-returns-status-invalid-request

    const placeSearchWrapper2 = await mapwrap.additionalPlaces(placeSearchWrapper.getNextPageToken());
    */

    const placeDetailsObject = await mapwrap.placeDetails("ChIJN1t_tDeuEmsRUsoyG83frY4");

    console.log(placeDetailsObject);

  } catch (err) {
    console.log(err);
  }
  
})();

If any bugs are found, please don't hesitate to open an issue on the project repo. I'll do my best to address them.