Socket
Socket
Sign inDemoInstall

feathers-google-maps

Package Overview
Dependencies
23
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    feathers-google-maps

A Feathers service for interacting with the Google Maps API


Version published
Weekly downloads
57
decreased by-22.97%
Maintainers
1
Install size
3.60 MB
Created
Weekly downloads
 

Readme

Source

feathers-google-maps

A Feathers service for Google Maps API

Installation

yarn add feathers-google-maps

Documentation

This service is a light wrapper around the @googlemaps/google-maps-services-js library. Please refer to the @googlemaps/google-maps-services-js documenation for options that can be passed. You will also find it helpful to reference the Google Maps Web Service APIs docs.

Available Services

The following methods are supported and map to the appropriate @google/maps methods:

  • directions
  • distancematrix
  • elevation
  • geocode
  • reverseGeocode
  • geolocate
  • placeAutocomplete
  • placeDetails
  • findPlaceFromText
  • placePhoto
  • placesNearby
  • placeQueryAutocomplete
  • textSearch
  • nearestRoads
  • snapToRoads
  • timezone

Creating a Service

const GoogleMapsService = require('feathers-google-maps');

const Directions = new GoogleMapsService({
  key: 'YOUR API KEY',
  method: 'directions'
});

app.use('directions', Directions);

The service exposes two methods, find(params) and create(data, params). These two methods are functionally equivalent. The create() method is added so that you can take advantage of the .on('created') service event.

When using the find(params) method, include the query as the params to be passed to the underlying GoogleMaps method.

app.service('directions').find({
  query: {
    origin: 'Nashville, TN',
    destination: 'Memphis, TN'
  }
});

// This will call the googlMaps method like
googleMaps.directions({
  params: {
    origin: 'Nashville, TN',
    destination: 'Memphis, TN',
    key: 'YOUR API KEY'
  }
});

When using the create(data, params) method, the data will be passed to the underlying GoogleMaps method.

app.service('directions').create({
  origin: 'Nashville, TN',
  destination: 'Memphis, TN'
});

// This will also call the googlMaps method like
googleMaps.directions({
  params: {
    origin: 'Nashville, TN',
    destination: 'Memphis, TN',
    key: 'YOUR API KEY'
  }
});

Complete Example

const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');
const GoogleMapsService = require('feathers-google-maps');

// Initialize the application
const app = feathers()
  .configure(express.rest())
  .configure(socketio())
  .use(express.json())
  .use(express.urlencoded({ extended: true }))
  .use(express.errorHandler({ html: false }));

// Create a directions service
app.use('directions', new GoogleMapsService({
  key: 'YOUR API KEY',
  method: 'directions'
}));

// Create a geocode service
app.use('geocode', new GoogleMapsService({
  key: 'YOUR API KEY',
  method: 'geocode'
}));

const directionsService = app.service('directions');
const geocodeService = app.service('geocode');

const successHook = context => {
  // On success, context.params.googleMaps will include a `response`
  console.log('Success: ', context.data, context.params.googleMaps);
}

const errorHook = context => {
  // On error, context.params.googleMaps will include an `error`
  console.log('Error: ', context.error, context.params.googleMaps);
}

directionsService.hooks(
  after: {
    find: [successHook]
    create: [successHook]
  },
  error: {
    find: [errorHook]
    create: [errorHook]
  },
);

directionsService.on('created', result => {
  console.log('Directions created event');
})

geocodeService.hooks(
  after: {
    find: [successHook]
    create: [successHook]
  },
  error: {
    find: [errorHook]
    create: [errorHook]
  },
);

geocodeService.on('created', result => {
  console.log('Geocode created event');
})

directionsService
  .find({ query: { origin: 'Nashville, TN', destination: 'Memphis, TN' } })
  .then(result => {
    console.log('Directions found', result);
  })
  .catch(error => {
    console.log('Error finding directions', error);
  });

directionsService
  .create({ origin: 'Nashville, TN', destination: 'Memphis, TN' })
  .then(result => {
    console.log('Directions created', result);
  })
  .catch(error => {
    console.log('Error creating directions', error);
  });

geocodeService
  .find({ query: { address: '1010 Forrest Ave, Nashville, TN' } })
  .then(result => {
    console.log('Geocode found', result);
  })
  .catch(error => {
    console.log('Error geocoding', error);
  });

geocodeService
  .create({ address: '1010 Forrest Ave, Nashville, TN' })
  .then(result => {
    console.log('Geocode found', result);
  })
  .catch(error => {
    console.log('Error geocoding', error);
  });

app.listen(3030);

console.log('Feathers app started on 127.0.0.1:3030');

License

Copyright (c) 2019

Licensed under the MIT license.

Keywords

FAQs

Last updated on 26 Jun 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc