@globalfishingwatch/react-map-gl
Version:
A React wrapper for MapboxGL-js and overlay API.
177 lines (162 loc) • 5.25 kB
JavaScript
import * as React from 'react';
import { useRef, useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { document } from '../utils/globals';
import mapboxgl from '../utils/mapboxgl';
import MapState from '../utils/map-state';
import TransitionManager from '../utils/transition-manager';
import { isGeolocationSupported } from '../utils/geolocate-utils';
import useMapControl, { mapControlDefaultProps, mapControlPropTypes } from './use-map-control';
const LINEAR_TRANSITION_PROPS = Object.assign({}, TransitionManager.defaultProps, {
transitionDuration: 500
});
const noop = () => {};
const propTypes = Object.assign({}, mapControlPropTypes, {
className: PropTypes.string,
style: PropTypes.object,
label: PropTypes.string,
auto: PropTypes.bool,
positionOptions: PropTypes.object,
fitBoundsOptions: PropTypes.object,
trackUserLocation: PropTypes.bool,
showUserLocation: PropTypes.bool,
showAccuracyCircle: PropTypes.bool,
onViewStateChange: PropTypes.func,
onViewportChange: PropTypes.func,
onGeolocate: PropTypes.func
});
const defaultProps = Object.assign({}, mapControlDefaultProps, {
className: '',
style: {},
label: 'Geolocate',
auto: false,
positionOptions: {
enableHighAccuracy: false,
timeout: 6000
},
fitBoundsOptions: {
maxZoom: 15
},
trackUserLocation: false,
showUserLocation: true,
showAccuracyCircle: true,
onGeolocate: () => {}
});
function getBounds(position) {
const center = new mapboxgl.LngLat(position.coords.longitude, position.coords.latitude);
const radius = position.coords.accuracy;
const bounds = center.toBounds(radius);
return [[bounds._ne.lng, bounds._ne.lat], [bounds._sw.lng, bounds._sw.lat]];
}
function setupMapboxGeolocateControl(context, props, geolocateButton) {
const control = new mapboxgl.GeolocateControl(props);
control._container = document.createElement('div');
control._map = {
on: () => {},
_getUIString: () => ''
};
control._setupUI(true);
control._geolocateButton = geolocateButton;
const {
eventManager
} = context;
if (control.options.trackUserLocation && eventManager) {
eventManager.on('panstart', () => {
if (control._watchState === 'ACTIVE_LOCK') {
control._watchState = 'BACKGROUND';
geolocateButton.classList.add('mapboxgl-ctrl-geolocate-background');
geolocateButton.classList.remove('mapboxgl-ctrl-geolocate-active');
}
});
}
control.on('geolocate', props.onGeolocate);
return control;
}
function triggerGeolocate(context, props, control) {
if (control) {
control._map = context.map;
control.options = props;
control.trigger();
}
}
function updateCamera(position, {
context,
props
}) {
const bounds = getBounds(position);
const {
longitude,
latitude,
zoom
} = context.viewport.fitBounds(bounds, props.fitBoundsOptions);
const newViewState = Object.assign({}, context.viewport, {
longitude,
latitude,
zoom
});
const mapState = new MapState(newViewState);
const viewState = Object.assign({}, mapState.getViewportProps(), LINEAR_TRANSITION_PROPS);
const onViewportChange = props.onViewportChange || context.onViewportChange || noop;
const onViewStateChange = props.onViewStateChange || context.onViewStateChange || noop;
onViewStateChange({
viewState
});
onViewportChange(viewState);
}
function GeolocateControl(props) {
const thisRef = useMapControl(props);
const {
context,
containerRef
} = thisRef;
const geolocateButtonRef = useRef(null);
const [mapboxGeolocateControl, createMapboxGeolocateControl] = useState(null);
const [supportsGeolocation, setSupportsGeolocation] = useState(false);
useEffect(() => {
let control;
isGeolocationSupported().then(result => {
setSupportsGeolocation(result);
if (geolocateButtonRef.current) {
control = setupMapboxGeolocateControl(context, props, geolocateButtonRef.current);
control._updateCamera = position => updateCamera(position, thisRef);
createMapboxGeolocateControl(control);
}
});
return () => {
control._clearWatch();
};
}, []);
useEffect(() => {
if (props.auto) {
triggerGeolocate(context, props, mapboxGeolocateControl);
}
}, [mapboxGeolocateControl, props.auto]);
const {
className,
style,
label,
trackUserLocation
} = props;
return React.createElement("div", null, React.createElement("div", {
key: "geolocate-control",
className: "mapboxgl-ctrl mapboxgl-ctrl-group ".concat(className),
ref: containerRef,
style: style
}, React.createElement("button", {
key: "geolocate",
className: "mapboxgl-ctrl-icon mapboxgl-ctrl-geolocate",
ref: geolocateButtonRef,
disabled: !supportsGeolocation,
"aria-pressed": !trackUserLocation,
type: "button",
title: label,
onClick: () => triggerGeolocate(context, props, mapboxGeolocateControl)
}, React.createElement("span", {
className: "mapboxgl-ctrl-icon",
"aria-hidden": "true"
}))));
}
GeolocateControl.propTypes = propTypes;
GeolocateControl.defaultProps = defaultProps;
export default GeolocateControl;
//# sourceMappingURL=geolocate-control.js.map