A promise-based implementation integration with Google Places for Node or server-side JS platforms. This cannot be used as a front-end solution (Angular/React/Vue/Vanilla, etc) because Google has provided their own client JS SDK
In v1 I have updated the library to return the full response body, allowing the developer to have access to anything the API sends back. This will give you access to the status
, next_page_token
, and the corresponding "entity" for each search. Typescript typings have been added as well. Please read the Release Notes for more information on this breaking change.
TypeScript Types Please note that the google docs aren't the best at describing the format of results you get from each endpoint. I have spend a good amount of time trying to make sure the response types are as accurate as possible. If something is not accurate, feel free to open a pull request explaining the inconsistency with the correction.
yarn add google-places-web;
npm i google-places-web -S
// ES6
import Places from "google-places-web";
// ES5
const Places: GooglePlaces = require("google-places-web").default; // instance of GooglePlaces Class;
// Setup
Places.apiKey = "<API_KEY>";
Places.debug = __DEV__; // boolean;
let partialAddress = "1600 Pennsylv";
const radius = 2000;
const language = "en";
// Search with default opts
Places.autocomplete({ input: partialAddress, radius, language })
.then(results => {
// results array of partial matches
})
.catch(e => console.log(e));
const whiteHousePlaceID = "ChIJGVtI4by3t4kRr51d_Qm_x58";
try {
const response = Places.details({ placeid: whiteHousePlaceID });
const { status, result } = response;
} catch (error) {
console.log(error);
}
try {
const response = await Places.nearbysearch({
location: "-37.814,144.96332", // LatLon delimited by ,
// radius: "500", // Radius cannot be used if rankBy set to DISTANCE
type: [], // Undefined type will return all types
rankby: "distance" // See google docs for different possible values
});
const { status, results, next_page_token, html_attributions } = response;
} catch (error) {
console.log(error);
}
try {
const response = await Places.textsearch({
query: "Pizza near me"
});
const { status, results } = response;
} catch (error) {
console.log(error);
}
Make sure you have the dependencies installed (yarn
) and have built it with yarn build
.
# will build and run the example, replace with your google api key
> PLACES_API_KEY=<your_key_here> yarn ts-node examples/whatever-example.ts
Feel free to file issues as you see fit, and always looking for collaborators to help make this better. If you are interested in contributing, you may clone the repository, and create a .ts
file for an example
Invalid API Key
- The instance of theGooglePlaces
object does not have a valid API key from Google. Make sure you are either usingimport Places from...
orimport {GooglePlaces} from...
.GooglePlaces
is the base class so you would need to make an instance of it first.STATUS_MESSAGE
- Google responds with HTTP 200 but JSON contains an "error". This is parsed from the Google API response, ex.ZERO_RESULTS
Missing required params: [<PARAM1>, <PARAM2>]
- Required params PARAM1 & PARAM2 areundefined
ornull
No parameters provided
- A method was called without passing a parameters object to the method, most likely passednull
,undefined
or nothing. ex.Places.autocomplete();
instead ofPlaces.autcomplete({foo: 'bar'});
Google states that you can use Place Autocomplete even without a map. If you do show a map, it must be a Google map. When you display predictions from the Place Autocomplete service without a map, you must include the Powered by Google logo.
- enhance TS support for the options allowed for each endpoint