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

google-places-autocomplete-service

v0.0.3

Published

A simple factory using google places API to fetch predictions for places and get place details.

Downloads

18

Readme

google-places-autocomplete-service

A simple factory using google places API to fetch predictions for places and get place details.

Installation

npm i -S google-places-autocomplete-service

Then add the Google Places API script to your page:

Usage

First add the dependency to your app:

import googlePlacesService from 'google-places-autocomplete-service';

Then initialize the service:

this.googlePlaces = googlePlacesService(options);

Then get predictions:

this.googlePlaces.getPredictions('example text', callback);

Then get place info:

this.googlePlaces.getPlace(predictionObject, callback);

And that's it.

Configuration

The service accepts some options on initialization, for example:

this.googlePlaces = googlePlacesService({
  type: 'geocode',
  filterByCountry: 'US',
  outputPlaceTypes: [
    'postalCode',
    'locality',
    'administrativeAreaLevel1',
    'country'
  ],
  searchStrategies: [
    'searchByPlaceId'
  ]
});

The type option:

This option will be used in getPredictions to filter predictions results - if none is passed geocode will be used as a default.

Valid types:

  • geocode
  • address
  • establishment
  • (regions)
  • (cities)

The filterByCountry option:

This option will be used in getPredictions to restrict the predictions results to a country. Accepts uppercase ISO Alpha-2 country code - if none is passed then it will output global predictions.

The outputPlaceTypes option:

This option will be used in getPlace to make sure it will return a place with those place info types included.

Valid types:

  • streetAddress
  • route
  • streetNumber
  • neighborhood
  • postalCode
  • sublocality
  • locality
  • administrativeAreaLevel1
  • administrativeAreaLevel2
  • administrativeAreaLevel3
  • administrativeAreaLevel4
  • administrativeAreaLevel5
  • country

The searchStrategies option:

This option will be used in getPlace and will search for the specified place with the strategies passed - if none is passed then it will use all the strategies needed.

Extra options:

  • longitude and latitude are optional, but required if use only searchWithGeocoder and nothing else.

getPredictions(input, callback, regex)

Retrieves place autocomplete predictions based on the supplied arguments. Return value: None

  • input: string - The user entered input string.
  • callback: function - What to do with the results i.e: (predictions) => console.log(predictions).
  • regex: regexp - (optional) A regex to filter out predictions.

The predictions object passing to the callback, is an object of placeIds as keys and the corresponding info of the prediction. Like that:

{
  ChIJOwg_06VPwokRYv534QaPC8g: {
    body: "New York, NY, United States"
    terms: [
      "New York",
      "NY",
      "United States"
    ],
    type: "locality"
  },
  ...
}

getPlace(prediction, callback)

Retrieves details about the place. Return value: None

  • prediction: object - the prediction :)
    • placeId: string - the prediction's place ID.
    • type: string - prediction's first of types.
    • terms: array - prediction's string tokens.
    • body: string - prediction's body.
  • callback: function - What to do with the result i.e: (placeInfo) => console.log(placeInfo).

The placeInfo passing to the callback, is an object with place's info like coords, prediction terms that couldn't find, status and of course all the place types that could find. For example:

{
  coords: "40.6781784, -73.9441579",
  sublocality: "Brooklyn",
  locality: "New York",
  administrativeAreaLevel1: "New York",
  administrativeAreaLevel1Code: "NY",
  country: "United States",
  notValid: [],
  status: "SUCCESS"
}

Issues or feature requests

Create a ticket here

Contributing

Issue a pull request.