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

Map Block: Persist Center Point #14417

Merged
merged 2 commits into from
Jan 22, 2020
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
33 changes: 24 additions & 9 deletions extensions/blocks/map/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,17 @@ export class Map extends Component {
this.setBoundsByMarkers();
};
setBoundsByMarkers = () => {
const { zoom, points, onSetZoom } = this.props;
const { admin, onSetMapCenter, onSetZoom, points, zoom } = this.props;
const { map, activeMarker, mapboxgl, zoomControl, boundsSetProgrammatically } = this.state;
if ( ! map ) {
return;
}
// Do not allow map dragging in the editor if there are markers, because the positioning will be programmatically overridden.
if ( points.length && admin ) {
map.dragPan.disable();
} else {
map.dragPan.enable();
}
// If there are no points at all, there is no data to set bounds to. Abort the function.
if ( ! points.length ) {
return;
Expand All @@ -198,6 +204,7 @@ export class Map extends Component {
points.forEach( point => {
bounds.extend( [ point.coordinates.longitude, point.coordinates.latitude ] );
} );
onSetMapCenter( bounds.getCenter() );

// If there are multiple points, zoom is determined by the area they cover, and zoom control is removed.
if ( points.length > 1 ) {
Expand Down Expand Up @@ -296,7 +303,13 @@ export class Map extends Component {
map.on( 'zoomend', () => {
this.props.onSetZoom( map.getZoom() );
} );

map.on( 'moveend', () => {
const { onSetMapCenter, points } = this.props;
// If there are no markers, user repositioning controls map center. If there are markers, set programmatically.
if ( points.length < 1 ) {
onSetMapCenter( map.getCenter() );
}
} );
/* Listen for clicks on the Map background, which hides the current popup. */
map.getCanvas().addEventListener( 'click', this.onMapClick );
this.setState( { map, zoomControl }, () => {
Expand All @@ -312,20 +325,22 @@ export class Map extends Component {
window.addEventListener( 'resize', this.debouncedSizeMap );
} );
}
googlePoint2Mapbox( google_point ) {
const mapCenter = [
google_point.longitude ? google_point.longitude : 0,
google_point.latitude ? google_point.latitude : 0,
];
return mapCenter;
}
googlePoint2Mapbox = google_point =>
google_point.hasOwnProperty( 'lat' ) && google_point.hasOwnProperty( 'lng' )
Copons marked this conversation as resolved.
Show resolved Hide resolved
? google_point // Already a valid Mapbox point.
: {
// Legacy point, supported here to avoid block deprecation.
lat: google_point.latitude || 0,
lng: google_point.longitude || 0,
};
}

Map.defaultProps = {
points: [],
mapStyle: 'default',
zoom: 13,
onSetZoom: () => {},
onSetMapCenter: () => {},
onMapLoaded: () => {},
onMarkerClick: () => {},
onError: () => {},
Expand Down
1 change: 1 addition & 0 deletions extensions/blocks/map/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class MapEdit extends Component {
admin={ true }
apiKey={ apiKey }
onSetPoints={ value => setAttributes( { points: value } ) }
onSetMapCenter={ value => setAttributes( { mapCenter: value } ) }
onMapLoaded={ () => this.setState( { addPointVisibility: true } ) }
onMarkerClick={ () => this.setState( { addPointVisibility: false } ) }
onError={ this.onError }
Expand Down