Skip to content

Commit

Permalink
feat(blocks-google-maps): Add onLoad to maps.
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanieJKS committed Jun 24, 2022
1 parent a85cb8d commit e9a75df
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/plugins/blocks/blocks-google-maps/src/blocks/Map.js
Expand Up @@ -39,15 +39,16 @@ const Map = ({ blockId, children, content, methods, properties }) => {

useEffect(() => {
methods.registerMethod('fitBounds', (args) => {
if (type.isArray(args)) {
args.map((arg) => {
bounds.extend(arg);
});
}
if (type.isObject(args)) {
bounds.extend(args);
if (!bounds || !map) {
throw new Error('fitBounds can only be called once google maps has been mounted.');
}
(args?.bounds ?? []).map((position) => {
bounds.extend(position);
});
map.fitBounds(bounds);
if (args.zoom) {
map.setZoom(args.zoom);
}
});
}, [bounds, map]);

Expand All @@ -71,9 +72,13 @@ const Map = ({ blockId, children, content, methods, properties }) => {
mapContainerClassName={methods.makeCssClass([STYLE_DEFAULTS, properties.style])}
center={properties.map?.center ?? MAP_DEFAULTS.center}
zoom={properties.map?.zoom ?? MAP_DEFAULTS.zoom}
onLoad={(map) => {
onLoad={(map, event) => {
setMap(map);
setBounds(new window.google.maps.LatLngBounds());
methods.triggerEvent({
name: 'onLoad',
event,
});
}}
onClick={(event) => {
methods.triggerEvent({
Expand Down

0 comments on commit e9a75df

Please sign in to comment.