Code Monkey home page Code Monkey logo

gatsby-source-googlemaps-static's Introduction

Gatsby Source Google Maps Static Plugin

GSGS (gatsby-source-googlemaps-static)

Gatsby

npm package Downloads dependencies Status npm type definitions DeepScan grade Code Inspector grade

This source plugin for Gatsby will make location information from Google Maps available in GraphQL queries and provide a link to open that map on Google Maps. GSGS (gatsby-source-googlemaps-static) plugin will also cache the image response and only make a call to the API when the cache is invalid or empty. The cache is invalidated when you change any of the values below (Omitting the key and secret).

GSGS will also obscure your API key by only holding onto the key, or secret, when an API call is necessary. After the key, or secret, has been deemed unnecessary, it will be deleted from memory.

Install

# Install the plugin
npm install @ccalamos/gatsby-source-googlemaps-static

# or using Yarn
yarn add @ccalamos/gatsby-source-googlemaps-static

How to use

In gatsby-config.js:

Minimum Setup

module.exports = {
    plugins: [
        {
            resolve: `@ccalamos/gatsby-source-googlemaps-static`,
            options: {
                key: `YOUR_GOOGLE_MAPS_STATIC_API_KEY`,
                center: `LATITUDE,LONGITUDE || CITY,REGION`,
            },
        },
    ],
};

NOTE: To get a Google Maps Static API key, register for a Google Maps dev account.

Options

The configuration options for this plugin are currently an expansive set of the static API parameters. Please review those docs for more details and feel free to contribute to this repo to expand the accepted parameters.

module.exports = {
    plugins: [
        {
            resolve: `@ccalamos/gatsby-source-googlemaps-static`,
            options: {
                key: `YOUR_GOOGLE_MAPS_STATIC_API_KEY`,
                center: `LATITUDE,LONGITUDE || CITY,REGION`,
                zoom: `ZOOM_LEVEL`,
                size: `SIZE || WIDTHxHEIGHT`,
                scale: `SCALE_VALUE`,
                format: `IMAGE_EXTENSION`,
                mapType: `MAP_FORMAT`,
                mapID: `CLOUD_BASED_STYLE_MAP_ID`,
                styles:
                    [
                        {
                            feature: `FEATURE`,
                            element: `ELEMENT`,
                            rules: {
                                hue: `HUE`,
                                lightness: `LIGHTNESS`,
                                saturation: `SATURATION`,
                                gamma: `GAMMA`,
                                invert_lightness: true || false,
                                visibility: `VISIBILITY`,
                                color: `COLOR`,
                                weight: `WEIGHT`,
                            },
                        },
                    ],
                markers:
                    [
                        {
                            location: `LATITUDE,LONGITUDE || CITY,REGION`,
                            color: `COLOR`,
                            size: `SIZE`,
                            label: `A_SINGLE_ALPHANUMERIC_CHARACTER`,
                            icon: `URI`,
                            anchor: `ANCHOR_POSITION`,
                        },
                    ],
                paths:
                    [
                        {
                            weight: `WEIGHT`,
                            color: `COLOR`,
                            fillColor: `FILL_COLOR`,
                            geoDesic: true || false,
                            points: [`LATITUDE,LONGITUDE || CITY,REGION`],
                        },
                    ],
                visible:
                    [`LATITUDE,LONGITUDE || CITY,REGION`],
                clientID: `GOOGLE_MAPS_STATIC_CLIENT_ID`,
                secret: `GOOGLE_MAPS_SECRET_FOR_SIGNED_URLS`,
                query: `GOOGLE_MAPS_URL_QUERY`,
                nickname: `NICKNAME`,
                maps: [
                    {
                        `ALL_OPTIONS`,
                    }
                ],
            },
        },
    ],
};
Option Default Description Notes
key [required**] Your application's API key. This key identifies your application for purposes of quota management. This is not required if you provide both clientID and secret
center [required*] The latitude and longitude or address of the center of the map. This is not required if you are using Implicit Mapping. See Google Maps Static Docs for more information.
zoom 14 The zoom level of the map.
size 640x640 The width and height of the image returned from Google. It is optional to include the x character if the height and width are equal.
scale The scale value for the map returned by Google.
format png The file format and extension for the image.
mapType The type of map that Google should use to render the image.
mapID The ID from Google Cloud Based Maps.
clientID [required**] The client ID from Google Cloud Platform Console. This field is not required when using key field.
secret [required*] The modified base64 secret from Google Cloud Platform Console used for signing URLs. This field is only required when attempting to create a signature.
query A custom query to replace your center for the generated map URL.
styles Provides custom styles to the returned image. Either a preformatted URI string or an Array of Objects. Please see Google Maps Static Styled Maps for more information about these fields.
markers Places markers on the return map image. Either a preformatted URI string or an Array of Objects. Please see Google Maps Static Markers for more information about these fields.
paths Places a path or a polygonal area over top of the map. Either a preformatted URI string or an Array of Objects. Please see Google Maps Static API Paths for more information about these fields.
visible Ensures that the provided points are visible on the map. Either a preformatted URI string or an Array of Strings. This has precedence over zoom level. Please see Google Maps Static Viewports for more information about these fields.
nickname Used to add a nickname to the map, for ease of use with GraphQL This field will default to using the hash ID if not specified.
maps Used to add multiple maps to gatsby. This field takes all the same options as the options field, however it will override the options field for that map.

** If provided with both key and clientID, **GSGS** will prefer to use clientID.

If map is generated using Implicit Mapping, then the generated URL will be using Google Directions Waypoints. The first paths points will be the waypoints, if that is not provided then it will use the markers locations, if that is not provided then it will use the visible locations.

Example Configuration

A Very Simple Configuration Example

module.exports = {
    plugins: [
        {
            resolve: "@ccalamos/gatsby-source-googlemaps-static",
            options: {
                key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
                center: "41.8781,-87.6298",
            },
        },
    ],
};

Another Very Simple Configuration Example

module.exports = {
    plugins: [
        {
            resolve: "@ccalamos/gatsby-source-googlemaps-static",
            options: {
                key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
                center: "Chicago, IL",
            },
        },
    ],
};

An Implicit Mapping Configuration Example

module.exports = {
    plugins: [
        {
            resolve: `@ccalamos/gatsby-source-googlemaps-static`,
            options: {
                key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
                paths: [
                    {
                        color: `0x00000000`,
                        weight: `5`,
                        fillColor: `0xFFFF0033`,
                        points: [
                            `8th Avenue & 34th St, New York, NY`,
                            `8th Avenue & 42nd St,New York,NY`,
                            `Park Ave & 42nd St,New York,NY,NY`,
                            `Park Ave & 34th St,New York,NY,NY`,
                        ],
                    },
                ],
            },
        },
    ],
};

Signature Configuration Example (Using API KEY)

module.exports = {
    plugins: [
        {
            resolve: `@ccalamos/gatsby-source-googlemaps-static`,
            options: {
                key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
                secret: process.env.GOOGLE_MAPS_STATIC_SECRET,
                center: `New York, NY`,
            },
        },
    ],
};

Signature Configuration Example (Using Client ID)

module.exports = {
    plugins: [
        {
            resolve: `@ccalamos/gatsby-source-googlemaps-static`,
            options: {
                clientID: process.env.GOOGLE_MAPS_STATIC_CLIENT_ID,
                secret: process.env.GOOGLE_MAPS_STATIC_SECRET,
                center: `New York, NY`,
            },
        },
    ],
};

Query Configuration Example

module.exports = {
    plugins: [
        {
            resolve: `@ccalamos/gatsby-source-googlemaps-static`,
            options: {
                key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
                center: `Chicago, IL`,
                query: `Willis Tower`,
            },
        },
    ],
};

Multiple Maps Example

module.exports = {
    plugins: [
        {
            resolve: `@ccalamos/gatsby-source-googlemaps-static`,
            options: {
                key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
                styles: [
                    {
                        feature: `poi`,
                        element: `labels`,
                        rules: {
                            visibility: `off`,
                        },
                    },
                ],
                maps: [
                    {
                        center: `Chicago, IL`,
                        query: `Willis Tower`,
                    },
                    {
                        center: `Colorado Springs, CO`,
                        query: `Garden of the Gods`,
                    },
                    {
                        center: `Miami, FL`,
                        nickname: `Beach`,
                    },
                ],
            },
        },
    ],
};

Cloud Based Map Style ID

{
    resolve: `@ccalamos/gatsby-source-googlemaps-static`,
    options: {
        key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
        center: `Chicago, IL`,
        mapID: `8f348d1b5a61d4bb`
    },
},

GraphQl Queries

Once the plugin is configured, one new query is available in GraphQL: staticMap.

Here’s an example query to load the image of a Static Map:

query StaticMapQuery {
    staticMap {
        childFile {
            childImageSharp {
              gatsbyImageData(layout: FIXED # or FLUID)
            }
        }
    }
}

Here’s an example query to get the generated Google Maps URL of the Static Map:

query StaticMapQuery {
    staticMap {
        mapUrl
    }
}

Here's an example of querying the multiple generated files to get one specific by the nickname:

query StaticMapQuery {
    allStaticMap(filter: { nickname: { eq: "Beach" } }) {
        edges {
            node {
                childFile {
                    childImageSharp {
                      gatsbyImageData(layout: FIXED # or FLUID)
                    }
                }
            }
        }
    }
}

If you are using format: 'gif' Image Sharp will not be able to process your image, however you can still access your cached/downloaded image like so:

query StaticMapGifQuery {
    staticMap {
        childFile {
            publicURL
        }
    }
}

See the Image Sharp Plugin or the GraphiQL UI for info on all returned fields.

gatsby-source-googlemaps-static's People

Contributors

ccalamos avatar dependabot[bot] avatar tomconder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

gatsby-source-googlemaps-static's Issues

[BUG] 403 when installing

Describe the bug
403 when adding gatsby-source-googlemaps-static to gatsby-config.js
To Reproduce
Steps to reproduce the behavior:

  1. npm install gatsby-source-googlemaps-static
  2. add follow to gatsby-config.js
    {
      resolve: `gatsby-source-googlemaps-static`,
      options: {
          key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
          center: {
            lat: xxxx,
            long: -xxxx,
          },
          zoom: 11,
      },
    },
  1. gatsby build
  2. Error received:
success open and validate gatsby-configs - 0.045s
success load plugins - 0.368s
success onPreInit - 0.007s
success delete html and css files from previous builds - 0.011s
success initialize cache - 0.013s
success copy gatsby files - 0.031s
success onPreBootstrap - 0.007s
success createSchemaCustomization - 0.004s

 ERROR #11321  PLUGIN

"gatsby-source-googlemaps-static" threw an error while running the sourceNodes lifecycle:

Request failed with status code 403



  Error: Request failed with status code 403
  
  - createError.js:16 createError
    [HiddenAcresRV]/[axios]/lib/core/createError.js:16:15
  
  - settle.js:17 settle
    [HiddenAcresRV]/[axios]/lib/core/settle.js:17:12
  
  - http.js:236 IncomingMessage.handleStreamEnd
    [HiddenAcresRV]/[axios]/lib/adapters/http.js:236:11
  
  - task_queues.js:84 processTicksAndRejections
    internal/process/task_queues.js:84:21
  

not finished source and transform nodes - 0.249s

Expected behavior
Successful response from build info Done building

Desktop:

  • OS: MacOS 10.15.7

Not working in Gatsby 4

When trying to install with Gatsby 5 get the following errors:

npm install --save @ccalamos/gatsby-source-googlemaps-static
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/gatsby
npm ERR! gatsby@"^4.4.0" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer gatsby@"^3.0.0" from @ccalamos/[email protected]
npm ERR! node_modules/@ccalamos/gatsby-source-googlemaps-static
npm ERR! @ccalamos/gatsby-source-googlemaps-static@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Error: Request failed with status code 403

I run npm install of the plugin, paste this to the gatsby-config:

      resolve: `gatsby-source-googlemaps-static`,
      options: {
        key: process.env.GOOGLE_MAPS_STATIC_API_KEY,
        center: `53.2547023,19.0905782`,

I also added API key to the .env file (which is working cause I have other API key there). Thing is when i run gatsby develop I got this:

 ERROR #11321  PLUGIN

"gatsby-source-googlemaps-static" threw an error while running the sourceNodes lifecycle:

Request failed with status code 403



  Error: Request failed with status code 403

  - createError.js:16 createError
    [gatsby-classic-theme]/[axios]/lib/core/createError.js:16:15

  - settle.js:17 settle
    [gatsby-classic-theme]/[axios]/lib/core/settle.js:17:12

  - http.js:236 IncomingMessage.handleStreamEnd
    [gatsby-classic-theme]/[axios]/lib/adapters/http.js:236:11

  - next_tick.js:63 process._tickCallback
    internal/process/next_tick.js:63:19


warn The gatsby-source-googlemaps-static plugin has generated no Gatsby nodes. Do you need it?

Should I provide more info or is this the plugin's fault?

Implement Markers

Implement the option to add markers in a JSON object or a preformatted param string

[BUG]I dont get any data returned, when I make a query.

Describe the bug
I created Google maps components in my Gatsby project and called the data using the Gatsby static Query plugin, sad enough I get no data in the console

To Reproduce
Steps to reproduce the behaviour:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See an error

Expected behaviour
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.
Screenshot (52)

Desktop (please complete the following information):

-OS [Windows]

  • Browser [Google chrome]

Smartphone (please complete the following information):

  • None

Additional context
Add any other context about the problem here.

Map error superimposed

Recently our map has been superimposed with a message on the top right "Map error: g.co/staticmaperror"

Nothing has changed, so it's probably something on Google's side.

Are others seeing this? Is there a way to see the HTTP headers as described on g.co/staticmaperror, with gatsby doing a pre-build?

Thanks

Mistake in Readme regarding GatsbyImageSharpFluid

In the readme, the query is shown as follows:

query StaticMapQuery {
    staticMap {
        childFile {
            childImageSharp {
                ...GatsbyImageSharpFluid
            }
        }
    }
}

It should be:

query StaticMapQuery {
    staticMap {
        childFile {
            childImageSharp {
                fluid { // this is missing
                    ...GatsbyImageSharpFluid
                }
            }
        }
    }
}

Updated README

Readme needs to be updated to reflect all possible configurations.

Implement Path

Implement the option to add a path as either a string or object

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.