Git Product home page Git Product logo

mapwrap's Introduction

MapWrap ๐Ÿ“Œ

npm version npm bundle size

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.

mapwrap's People

Contributors

pixeltopic avatar dependabot-preview[bot] avatar

Watchers

James Cloos avatar

mapwrap's Issues

Custom logger support for logCache

Alternative logger function support for MapWrap. If user passes in a function, mapwrap logs cache hits via that instead of the default console.log.

Allow configuration of cache

Users should be able to configure expiration time of cache items. User gives a time in milliseconds. Do not allow negative input nor input over 30 days

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.