Gatsby source plugin for the Google places API
npm install gatsby-source-google-placesSource plugin for pulling data from the Google Places API.
Get a Google Places API Key.
``shell`
npm install --save gatsby-source-google-placesor
yarn add gatsby-source-google-places
Setup the plugin
`jsgatsby-source-google-places
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: ,`
options: {
placeIds: ["
apiKey: "
language: "en-US", // optional, defaults to en-US
},
},
],
};
Query and display a single place
`js
// pages/places.js
import React from "react";
import { graphql } from "gatsby";
import Layout from "../components/layout";
const PlacesPage = ({ data }) => {
const place = data.googlePlacesPlace;
const reviews = place.childrenGooglePlacesReview.map((r) => (
{${r.text.substring(0, 250)} ...}
total ratings: {place.user_ratings_total}
average: {place.rating}
export const query = graphql
query {
googlePlacesPlace {
name
rating
childrenGooglePlacesReview {
author_name
text
rating
profile_photo_url
}
user_ratings_total
}
};
export default PlacesPage;
`
Query and display from multiple places in a page
`js
// pages/multiple-places
import React from "react";
import { graphql } from "gatsby";
import Layout from "../components/layout";
const PlacesPage = ({ data }) => {
const places = data.allGooglePlacesPlace.nodes.map((p) => (
{r.text}
export const query = graphql
query {
allGooglePlacesPlace {
nodes {
name
user_ratings_total
place_id
childrenGooglePlacesReview {
author_name
rating
profile_photo_url
text
}
}
}
};
export default PlacesPage;
``
Issues, suggestions and PRs welcome!