aboutsummaryrefslogtreecommitdiff
path: root/modern/src/map/PositionsMap.js
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-05-22 09:57:51 -0700
committerAnton Tananaev <anton@traccar.org>2022-05-22 09:57:51 -0700
commit67d7ce8e0f428c521d3d032e8145108c9b2a5f45 (patch)
tree9f350497ebc64951ce2070723ab2b66e1a0f5472 /modern/src/map/PositionsMap.js
parent111347e3f40640aa4c43c0bc2666d7a92ce03bb2 (diff)
downloadtrackermap-web-67d7ce8e0f428c521d3d032e8145108c9b2a5f45.tar.gz
trackermap-web-67d7ce8e0f428c521d3d032e8145108c9b2a5f45.tar.bz2
trackermap-web-67d7ce8e0f428c521d3d032e8145108c9b2a5f45.zip
Rename map plugins
Diffstat (limited to 'modern/src/map/PositionsMap.js')
-rw-r--r--modern/src/map/PositionsMap.js142
1 files changed, 0 insertions, 142 deletions
diff --git a/modern/src/map/PositionsMap.js b/modern/src/map/PositionsMap.js
deleted file mode 100644
index 2e152d34..00000000
--- a/modern/src/map/PositionsMap.js
+++ /dev/null
@@ -1,142 +0,0 @@
-import { useCallback, useEffect } from 'react';
-import { useSelector } from 'react-redux';
-
-import { map } from './core/Map';
-import { getStatusColor } from '../common/util/formatter';
-import usePersistedState from '../common/util/usePersistedState';
-
-const PositionsMap = ({ positions, onClick }) => {
- const id = 'positions';
- const clusters = `${id}-clusters`;
-
- const devices = useSelector((state) => state.devices.items);
-
- const [mapCluster] = usePersistedState('mapCluster', true);
-
- const createFeature = (devices, position) => {
- const device = devices[position.deviceId];
- return {
- id: position.id,
- deviceId: position.deviceId,
- name: device.name,
- category: device.category || 'default',
- color: getStatusColor(device.status),
- };
- };
-
- const onMouseEnter = () => map.getCanvas().style.cursor = 'pointer';
- const onMouseLeave = () => map.getCanvas().style.cursor = '';
-
- const onMarkerClick = useCallback((event) => {
- const feature = event.features[0];
- if (onClick) {
- onClick(feature.properties.id, feature.properties.deviceId);
- }
- }, [onClick]);
-
- const onClusterClick = useCallback((event) => {
- const features = map.queryRenderedFeatures(event.point, {
- layers: [clusters],
- });
- const clusterId = features[0].properties.cluster_id;
- map.getSource(id).getClusterExpansionZoom(clusterId, (error, zoom) => {
- if (!error) {
- map.easeTo({
- center: features[0].geometry.coordinates,
- zoom,
- });
- }
- });
- }, [clusters]);
-
- useEffect(() => {
- map.addSource(id, {
- type: 'geojson',
- data: {
- type: 'FeatureCollection',
- features: [],
- },
- cluster: mapCluster,
- clusterMaxZoom: 14,
- clusterRadius: 50,
- });
- map.addLayer({
- id,
- type: 'symbol',
- source: id,
- filter: ['!', ['has', 'point_count']],
- layout: {
- 'icon-image': '{category}-{color}',
- 'icon-allow-overlap': true,
- 'text-field': '{name}',
- 'text-allow-overlap': true,
- 'text-anchor': 'bottom',
- 'text-offset': [0, -2],
- 'text-font': ['Roboto Regular'],
- 'text-size': 12,
- },
- paint: {
- 'text-halo-color': 'white',
- 'text-halo-width': 1,
- },
- });
- map.addLayer({
- id: clusters,
- type: 'symbol',
- source: id,
- filter: ['has', 'point_count'],
- layout: {
- 'icon-image': 'background',
- 'text-field': '{point_count_abbreviated}',
- 'text-font': ['Roboto Regular'],
- 'text-size': 14,
- },
- });
-
- map.on('mouseenter', id, onMouseEnter);
- map.on('mouseleave', id, onMouseLeave);
- map.on('mouseenter', clusters, onMouseEnter);
- map.on('mouseleave', clusters, onMouseLeave);
- map.on('click', id, onMarkerClick);
- map.on('click', clusters, onClusterClick);
-
- return () => {
- Array.from(map.getContainer().getElementsByClassName('maplibregl-popup')).forEach((el) => el.remove());
-
- map.off('mouseenter', id, onMouseEnter);
- map.off('mouseleave', id, onMouseLeave);
- map.off('mouseenter', clusters, onMouseEnter);
- map.off('mouseleave', clusters, onMouseLeave);
- map.off('click', id, onMarkerClick);
- map.off('click', clusters, onClusterClick);
-
- if (map.getLayer(id)) {
- map.removeLayer(id);
- }
- if (map.getLayer(clusters)) {
- map.removeLayer(clusters);
- }
- if (map.getSource(id)) {
- map.removeSource(id);
- }
- };
- }, [mapCluster, clusters, onMarkerClick, onClusterClick]);
-
- useEffect(() => {
- map.getSource(id).setData({
- type: 'FeatureCollection',
- features: positions.filter((it) => devices.hasOwnProperty(it.deviceId)).map((position) => ({
- type: 'Feature',
- geometry: {
- type: 'Point',
- coordinates: [position.longitude, position.latitude],
- },
- properties: createFeature(devices, position),
- })),
- });
- }, [devices, positions]);
-
- return null;
-};
-
-export default PositionsMap;