File size: 3,799 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | import React, { Component, createRef } from 'react';
import PropTypes from 'prop-types';
import { createClassNames } from 'react-instantsearch-dom';
import { LatLngPropType, BoundingBoxPropType } from './propTypes';
import GoogleMapsContext from './GoogleMapsContext';
const cx = createClassNames('GeoSearch');
class GoogleMaps extends Component {
static propTypes = {
google: PropTypes.object.isRequired,
initialZoom: PropTypes.number.isRequired,
initialPosition: LatLngPropType.isRequired,
mapOptions: PropTypes.object.isRequired,
onChange: PropTypes.func.isRequired,
onIdle: PropTypes.func.isRequired,
shouldUpdate: PropTypes.func.isRequired,
boundingBox: BoundingBoxPropType,
boundingBoxPadding: PropTypes.number,
children: PropTypes.node,
};
isUserInteraction = true;
isPendingRefine = false;
listeners = [];
mapRef = createRef();
state = {
isMapReady: false,
};
componentDidMount() {
const { google, mapOptions } = this.props;
this.instance = new google.maps.Map(this.mapRef.current, {
mapTypeControl: false,
fullscreenControl: false,
streetViewControl: false,
clickableIcons: false,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_TOP,
},
...mapOptions,
});
this.listeners.push(
google.maps.event.addListenerOnce(
this.instance,
'idle',
this.setupListenersWhenMapIsReady
)
);
}
componentDidUpdate() {
const {
google,
initialZoom,
initialPosition,
boundingBox,
boundingBoxPadding,
shouldUpdate,
} = this.props;
if (!shouldUpdate()) {
return;
}
if (boundingBox) {
this.lockUserInteraction(() => {
const oldBounds = this.instance.getBounds();
// south west and north east are swapped
const newBounds = new google.maps.LatLngBounds(
boundingBox.southWest,
boundingBox.northEast
);
if (!newBounds.equals(oldBounds)) {
this.instance.fitBounds(newBounds, boundingBoxPadding);
}
});
} else {
this.lockUserInteraction(() => {
this.instance.setZoom(initialZoom);
this.instance.setCenter(initialPosition);
});
}
}
componentWillUnmount() {
this.listeners.forEach((listener) => {
listener.remove();
});
this.listeners = [];
}
setupListenersWhenMapIsReady = () => {
this.listeners = [];
this.setState({
isMapReady: true,
value: {
google: this.props.google,
instance: this.instance,
},
});
const onChange = () => {
if (this.isUserInteraction) {
this.props.onChange();
}
};
this.listeners.push(this.instance.addListener('center_changed', onChange));
this.listeners.push(this.instance.addListener('zoom_changed', onChange));
this.listeners.push(this.instance.addListener('dragstart', onChange));
this.listeners.push(
this.instance.addListener('idle', () => {
if (this.isUserInteraction) {
this.props.onIdle({
instance: this.instance,
});
}
})
);
};
lockUserInteraction(functionThatAltersTheMapPosition) {
this.isUserInteraction = false;
functionThatAltersTheMapPosition();
this.isUserInteraction = true;
}
render() {
const { children } = this.props;
const { isMapReady } = this.state;
return (
<div className={cx('')}>
<div ref={this.mapRef} className={cx('map')} />
{isMapReady && (
<GoogleMapsContext.Provider value={this.state.value}>
{children}
</GoogleMapsContext.Provider>
)}
</div>
);
}
}
export default GoogleMaps;
|