This custom field allows you to add locations to any content-type. Simply begin typing your locations and select it from an autocomplete dropdown list. The autocomplete functionality is powered by the Google Places API, which requires an API key.
This is a plugin for Strapi that adds a custom location field. Simply type in a location and select it from an autocomplete dropdown list. The autocomplete functionality is powered by the Google Places API, which requires an API key.
Strapi Interface
API Response
To install this package, run the following command in an existing strapi project:
npm install strapi-location-field-plugin
To enable the plugin, you'll need to include the following code in your Strapi project, in the /config/plugins.js
file:
1module.exports = ({ env }) => ({
2 "location-field": {
3 enabled: true,
4 config: {
5 fields: ["photo", "rating"], // optional
6 // You need to enable "Autocomplete API" and "Places API" in your Google Cloud Console
7 googleMapsApiKey: env("GOOGLE_MAPS_API_KEY"),
8 // See https://developers.google.com/maps/documentation/javascript/reference/places-autocomplete-service#AutocompletionRequest
9 autocompletionRequestOptions: {},
10 },
11 },
12 // .. your other plugin configurations
13});
Note: the config.fields
value can be set to an array of options (strings) containing any fields you'd like to be returned. The options that Google allows can be found here or in the screenshot below. When set, the information relevant to those specific fields will be accessible in the API response under the "details" key. The geometry
field is always enabled.
ℹ️ Please note: some fields may not return the expected response. Currently only the
photo
,rating
, andgeometry
fields have been tested.
autocompletionRequestOptions
allows you to customize the search behavior by overriding the options used when autocompletion requests are made. The Autocomplete API documentation lists all the available options. For example, this configuration returns autocomplete results in Spanish and biases the search closer to the caller's IP address:
1{
2 config: {
3 googleMapsApiKey: env("GOOGLE_MAPS_API_KEY"),
4 autocompletionRequestOptions: {
5 language: 'es',
6 locationBias: 'IP_BIAS',
7 },
8 },
9}
You'll also need to modify the /config/middlewares.js
file
1module.exports = [
2 "strapi::errors",
3 {
4 name: "strapi::security",
5 config: {
6 contentSecurityPolicy: {
7 directives: { "script-src": ["'self'", "'unsafe-inline'", "maps.googleapis.com"] },
8 },
9 },
10 },
11 "strapi::cors",
12 "strapi::poweredBy",
13 "strapi::logger",
14 "strapi::query",
15 "strapi::body",
16 "strapi::session",
17 "strapi::favicon",
18 "strapi::public",
19];
After installation and configuration, build the project with npm run build
or yarn build
To support, go to https://www.buymeacoffee.com/raykeating 💜
npm install strapi-location-field-plugin
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.