Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geocode the address attribute if it's present #29394

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Mapkit support for the address attribute
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ MapBoxComponent.defaultProps = {
markerColor: 'red',
apiKey: null,
mapCenter: {},
address: null,
};

export default MapBoxComponent;
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
useMapkitOnMapTap,
useMapkitZoom,
useMapkitPoints,
useMapkitAddressLookup,
} from '../mapkit/hooks';
import { createCalloutElementCallback } from '../mapkit-utils';
import InfoWindow from './info-window';
Expand Down Expand Up @@ -53,11 +54,13 @@ const MapkitComponent = forwardRef( ( props, mapRef ) => {

const MapkitHelpers = memo(
( {
address,
mapCenter,
mapStyle,
zoom,
onSetMapCenter,
onSetZoom,
onSetPoints,
points,
markerColor,
onMarkerClick,
Expand All @@ -73,6 +76,7 @@ const MapkitHelpers = memo(
} = useMapkit();
// Save these in a ref to prevent unwanted rerenders
const onMarkerClickRef = useRef( onMarkerClick );
const onSetPointsRef = useRef( onSetPoints );

const onSelect = useCallback(
marker => {
Expand Down Expand Up @@ -104,6 +108,8 @@ const MapkitHelpers = memo(
map.setCenterAnimated( previousCenter );
}
} );

useMapkitAddressLookup( address, onSetPointsRef );
return null;
}
);
Expand All @@ -119,6 +125,7 @@ MapkitComponent.defaultProps = {
onError: () => {},
markerColor: 'red',
mapCenter: {},
address: null,
};

export default MapkitComponent;
2 changes: 2 additions & 0 deletions projects/plugins/jetpack/extensions/blocks/map/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class MapEdit extends Component {
onResizeStart,
} = this.props;
const {
address,
mapDetails,
points,
zoom,
Expand Down Expand Up @@ -350,6 +351,7 @@ class MapEdit extends Component {
<div className="wp-block-jetpack-map__map_wrapper">
<Map
ref={ this.mapRef }
address={ address }
scrollToZoom={ allowScrollToZoom }
showFullscreenButton={ showFullscreenButton }
mapStyle={ mapStyle || 'default' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,37 @@ const useMapkitOnMapTap = onMapTap => {
}, [ loaded, map, previousCenter, onMapTapRef ] );
};

const useMapkitAddressLookup = ( address, onSetPointsRef ) => {
const { mapkit, map } = useMapkit();

useEffect( () => {
if ( mapkit && map && address?.length ) {
const geocoder = new mapkit.Geocoder();
geocoder.lookup( address, ( error, data ) => {
if ( data?.results?.length ) {
const place = data.results[ 0 ];
const title = place.formattedAddress;
const point = {
placeTitle: title,
title: title,
caption: title,
coordinates: {
longitude: place.coordinate.longitude,
latitude: place.coordinate.latitude,
},
// mapkit doesn't give us an id, so we'll make one containing the place name and coordinates
id: `${ title } ${ Number( place.coordinate.latitude ).toFixed( 2 ) } ${ Number(
place.coordinate.longitude
).toFixed( 2 ) }`,
};

onSetPointsRef.current( [ point ] );
}
} );
}
}, [ mapkit, map, address, onSetPointsRef ] );
};

export {
useMapkit,
useMapkitSetup,
Expand All @@ -245,4 +276,5 @@ export {
useMapkitPoints,
useMapkitOnMapLoad,
useMapkitOnMapTap,
useMapkitAddressLookup,
};