UNPKG

@globalfishingwatch/react-map-gl

Version:

A React wrapper for MapboxGL-js and overlay API.

187 lines (167 loc) 5.95 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; 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'; var LINEAR_TRANSITION_PROPS = Object.assign({}, TransitionManager.defaultProps, { transitionDuration: 500 }); var noop = function noop() {}; var 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 }); var 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 onGeolocate() {} }); function getBounds(position) { var center = new mapboxgl.LngLat(position.coords.longitude, position.coords.latitude); var radius = position.coords.accuracy; var bounds = center.toBounds(radius); return [[bounds._ne.lng, bounds._ne.lat], [bounds._sw.lng, bounds._sw.lat]]; } function setupMapboxGeolocateControl(context, props, geolocateButton) { var control = new mapboxgl.GeolocateControl(props); control._container = document.createElement('div'); control._map = { on: function on() {}, _getUIString: function _getUIString() { return ''; } }; control._setupUI(true); control._geolocateButton = geolocateButton; var eventManager = context.eventManager; if (control.options.trackUserLocation && eventManager) { eventManager.on('panstart', function () { 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, _ref) { var context = _ref.context, props = _ref.props; var bounds = getBounds(position); var _context$viewport$fit = context.viewport.fitBounds(bounds, props.fitBoundsOptions), longitude = _context$viewport$fit.longitude, latitude = _context$viewport$fit.latitude, zoom = _context$viewport$fit.zoom; var newViewState = Object.assign({}, context.viewport, { longitude: longitude, latitude: latitude, zoom: zoom }); var mapState = new MapState(newViewState); var viewState = Object.assign({}, mapState.getViewportProps(), LINEAR_TRANSITION_PROPS); var onViewportChange = props.onViewportChange || context.onViewportChange || noop; var onViewStateChange = props.onViewStateChange || context.onViewStateChange || noop; onViewStateChange({ viewState: viewState }); onViewportChange(viewState); } function GeolocateControl(props) { var thisRef = useMapControl(props); var context = thisRef.context, containerRef = thisRef.containerRef; var geolocateButtonRef = useRef(null); var _useState = useState(null), _useState2 = _slicedToArray(_useState, 2), mapboxGeolocateControl = _useState2[0], createMapboxGeolocateControl = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), supportsGeolocation = _useState4[0], setSupportsGeolocation = _useState4[1]; useEffect(function () { var control; isGeolocationSupported().then(function (result) { setSupportsGeolocation(result); if (geolocateButtonRef.current) { control = setupMapboxGeolocateControl(context, props, geolocateButtonRef.current); control._updateCamera = function (position) { return updateCamera(position, thisRef); }; createMapboxGeolocateControl(control); } }); return function () { control._clearWatch(); }; }, []); useEffect(function () { if (props.auto) { triggerGeolocate(context, props, mapboxGeolocateControl); } }, [mapboxGeolocateControl, props.auto]); var className = props.className, style = props.style, label = props.label, trackUserLocation = props.trackUserLocation; 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: function onClick() { return 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