A geocoding plugin for Payload CMS that simplifies location management in your content. This plugin allows you to easily populate coordinates in a Payload Point Field by entering an address through an autocomplete interface powered by the Google Places API.
Add the plugin to your payload config as follows:
plugins: [payloadGeocodingPlugin({})]
To use this plugin, you'll need a Google Maps API key. To get one, follow these steps:
- Set up a Google Cloud account and create a project
- Enable the Maps JavaScript API in your Google Cloud project
- Create an API key with Maps JavaScript API access
- Add the API key to your environment variables.
To add geocoding functionality to a point field, you can simply wrap the point field with the geocodingField
function:
geocodingField({
pointField: {
name: 'location',
type: 'point',
},
})
This will add a location_googlePlacesData
JSON field to the collection. This field will store the raw geocoding data from the Google Places API.
If needed you can adjust the location_googlePlacesData
field name by passing a name
property to the geocodingField
function.
geocodingField({
pointField: {
name: 'location',
type: 'point',
},
geoDataFieldOverride: {
label: 'JSON Geodata Field',
access: {
read: () => true,
update: () => true,
create: () => true,
},
admin: {
readOnly: false,
description: 'This field stores the geocoding data from the Google Places API.',
},
},
}),
This plugin uses the react-google-places-autocomplete library to provide a Select/Search input for finding an address. The result of the Google Places API request is stored in a JSON field and the coordinates are stored in a Point Field.
⚠️ Warning: This plugin is actively evolving and may undergo significant changes. While it is functional, please thoroughly test before using in production environments.
- Use the native Payload
SelectField
instead of the field provided byreact-google-places-autocomplete
- Extend the field config to accept
GooglePlacesAutocomplete
options like debounce time, API options, etc. - Add support for other geocoding services (Mapbox, HERE, etc.)
Have a suggestion for the plugin? Any feedback is welcome!
We welcome contributions! Please open an issue to report bugs or suggest improvements, or submit a pull request with your changes.