// Import core WordPress and React dependencies import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; import { Panel, PanelBody, PanelRow, Dropdown, Button, BaseControl, TextControl, RadioControl, ComboboxControl } from '@wordpress/components'; import { useFocusableIframe } from '@wordpress/compose'; import { BlockEditProps } from '@wordpress/blocks'; import { useRef, useEffect } from 'react'; // Import utilities import { getMapUrl, getMapObject, initializeMap } from './utilities'; // Import types import { MapSettings } from './types'; // Import option templates for each map mode import PlaceControls from './map-modes/place'; import ViewControls from './map-modes/view'; import DirectionsControls from './map-modes/directions'; import StreetviewControls from './map-modes/streetview'; import SearchControls from './map-modes/search'; import ThemedControls from './map-modes/themed'; // Import options for comboboxes import languageOptions from './options/languages'; import regionOptions from './options/regions'; // Import styles import './editor.scss'; // Define the block's interface in the editor const edit: React.ComponentType> = function ({ attributes, setAttributes }) { const blockProps = useBlockProps(); const iframeRef = useFocusableIframe() as React.LegacyRef; const containerRef = useRef(null); const apiKeyHelpInner =

Here's how to create your own API key:

  1. Go to the Google Maps Platform > Credentials page. If you haven't already, you may need to create an account and a Google Cloud project here.
  2. On the Credentials page, click Create credentials > API key. The API key created dialog displays your newly created API key.
  3. Copy the new API key and paste it in the field above.
  4. Go to the Google Maps Embed API page, ensure that the correct project is selected, and click Enable.
  5. If you're using the themed map mode, go to the Google Maps JavaScript API page, ensure that the correct project is selected, and click Enable. This is a pay-as-you-go API, so be aware that you may incur charges.
  6. Optionally, you can restrict your API keys to improve security. Google strongly recommends this.

*Loading multiple maps with different API keys onto the same web page may cause errors.

const apiKeyHelp =

Please create your own API key before publishing a map. This is a Google requirement, and themed maps won't work without it.

( ) } renderContent={ () => apiKeyHelpInner } />
; useEffect(() => { if (containerRef.current) getMapObject(attributes.key, containerRef.current, attributes.language, attributes.region) .then(map => initializeMap(map, attributes)); }); return ( <>
{ attributes.mapmode === 'themed' ? attributes.key === 'AIzaSyCRspsEADhOoOF4c2LhYKu_IAB0orV9ExA' ?

You must set your own Google Maps API key and enable the Google Maps JavaScript API to use themed maps.

:
: }
setAttributes({ key }) } /> setAttributes({ height: parseInt(event.target.value) }) } type="number" min={0} style={{ display: 'block', width: '100%', borderRadius: '2px' }} /> setAttributes({ mapmode }) } /> { attributes.mapmode === 'place' && } { attributes.mapmode === 'view' && } { attributes.mapmode === 'directions' && } { attributes.mapmode === 'streetview' && } { attributes.mapmode === 'search' && } { attributes.mapmode === 'themed' && }

Defines the language to use for UI elements and for the display of labels on map tiles. By default, visitors will see a map in their own language.

*Loading multiple maps with different language settings onto the same web page may cause errors.

} value={ attributes.language } onChange={ ( language ) => setAttributes({ language: language ?? '' }) } options={ languageOptions } />

Defines the appropriate borders and labels to display, based on geo-political sensitivities. Google's coverage varies by region

*Loading multiple maps with different region settings onto the same web page may cause errors.

} value={ attributes.region } onChange={ ( region ) => setAttributes({ region: region ?? '' }) } options={ regionOptions } />
); }; export default edit;