-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
keep old behaviour in RoutePath when we just have coordinates (and no… #1250
Changes from 1 commit
02a7e05
870ef74
28d8a5b
27f6cc5
244028c
abe03b6
1c779e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { useTheme } from '@mui/styles'; | ||
import { useId, useEffect } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { map } from './core/MapView'; | ||
|
||
const MapRouteCoordinates = ({ name, coordinates, deviceId }) => { | ||
const id = useId(); | ||
|
||
const theme = useTheme(); | ||
|
||
const reportColor = useSelector((state) => { | ||
const attributes = state.devices.items[deviceId]?.attributes; | ||
if (attributes) { | ||
const color = attributes['web.reportColor']; | ||
if (color) { | ||
return color; | ||
} | ||
} | ||
return theme.palette.geometry.main; | ||
}); | ||
|
||
useEffect(() => { | ||
map.addSource(id, { | ||
type: 'geojson', | ||
data: { | ||
type: 'Feature', | ||
geometry: { | ||
type: 'LineString', | ||
coordinates: [], | ||
}, | ||
}, | ||
}); | ||
map.addLayer({ | ||
source: id, | ||
id: `${id}-line`, | ||
type: 'line', | ||
layout: { | ||
'line-join': 'round', | ||
'line-cap': 'round', | ||
}, | ||
paint: { | ||
'line-color': ['get', 'color'], | ||
'line-width': 2, | ||
}, | ||
}); | ||
if (name) { | ||
map.addLayer({ | ||
source: id, | ||
id: `${id}-title`, | ||
type: 'symbol', | ||
layout: { | ||
'text-field': '{name}', | ||
'text-size': 12, | ||
}, | ||
paint: { | ||
'text-halo-color': 'white', | ||
'text-halo-width': 1, | ||
}, | ||
}); | ||
} | ||
|
||
return () => { | ||
if (map.getLayer(`${id}-title`)) { | ||
map.removeLayer(`${id}-title`); | ||
} | ||
if (map.getLayer(`${id}-line`)) { | ||
map.removeLayer(`${id}-line`); | ||
} | ||
if (map.getSource(id)) { | ||
map.removeSource(id); | ||
} | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
map.getSource(id)?.setData({ | ||
type: 'Feature', | ||
geometry: { | ||
type: 'LineString', | ||
coordinates, | ||
}, | ||
properties: { | ||
name, | ||
color: reportColor, | ||
}, | ||
}); | ||
}, [theme, coordinates]); | ||
|
||
return null; | ||
}; | ||
|
||
export default MapRouteCoordinates; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,13 @@ | ||
import { useTheme } from '@mui/styles'; | ||
import { useId, useEffect } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { map } from './core/MapView'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please check the warnings and errors. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have any warnings |
||
import { getSpeedColor } from '../common/util/colors'; | ||
Check warning on line 4 in src/map/MapRoutePath.js GitHub Actions / build
|
||
|
||
const MapRoutePath = ({ name, positions, coordinates }) => { | ||
const MapRoutePath = ({ name, positions }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like |
||
const id = useId(); | ||
|
||
const theme = useTheme(); | ||
|
||
const reportColor = useSelector((state) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we removing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the color is based on the position speed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but it means you're ignoring the settings. |
||
const position = positions?.find(() => true); | ||
if (position) { | ||
const attributes = state.devices.items[position.deviceId]?.attributes; | ||
if (attributes) { | ||
const color = attributes['web.reportColor']; | ||
if (color) { | ||
return color; | ||
} | ||
} | ||
} | ||
return theme.palette.geometry.main; | ||
}); | ||
|
||
useEffect(() => { | ||
map.addSource(id, { | ||
type: 'geojson', | ||
|
@@ -77,50 +62,36 @@ | |
}, []); | ||
|
||
useEffect(() => { | ||
if (!coordinates) { | ||
coordinates = positions.map((item) => [item.longitude, item.latitude]); | ||
const speeds = positions.map((item) => item.speed); | ||
const maxSpeed = speeds.reduce((a, b) => Math.max(a, b), -Infinity); | ||
const features = []; | ||
for (let i = 0; i < coordinates.length - 1; i += 1) { | ||
features.push({ | ||
type: 'Feature', | ||
geometry: { | ||
type: 'LineString', | ||
coordinates: [coordinates[i], coordinates[i + 1]], | ||
}, | ||
properties: { | ||
color: getSpeedColor( | ||
theme.palette.success.main, | ||
theme.palette.warning.main, | ||
theme.palette.error.main, | ||
speeds[i + 1], | ||
maxSpeed, | ||
), | ||
}, | ||
}); | ||
} | ||
map.getSource(id)?.setData({ | ||
type: 'FeatureCollection', | ||
features, | ||
properties: { | ||
name, | ||
}, | ||
}); | ||
} else { | ||
map.getSource(id)?.setData({ | ||
const coordinates = positions.map((item) => [item.longitude, item.latitude]); | ||
const speeds = positions.map((item) => item.speed); | ||
const maxSpeed = speeds.reduce((a, b) => Math.max(a, b), -Infinity); | ||
const features = []; | ||
for (let i = 0; i < coordinates.length - 1; i += 1) { | ||
features.push({ | ||
type: 'Feature', | ||
geometry: { | ||
type: 'LineString', | ||
coordinates, | ||
coordinates: [coordinates[i], coordinates[i + 1]], | ||
}, | ||
properties: { | ||
name, | ||
color: reportColor, | ||
color: getSpeedColor( | ||
theme.palette.success.main, | ||
theme.palette.warning.main, | ||
theme.palette.error.main, | ||
speeds[i + 1], | ||
maxSpeed, | ||
), | ||
}, | ||
}); | ||
} | ||
}, [theme, positions, coordinates, reportColor]); | ||
map.getSource(id)?.setData({ | ||
type: 'FeatureCollection', | ||
features, | ||
properties: { | ||
name, | ||
}, | ||
}); | ||
}, [theme, positions]); | ||
|
||
return null; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this one the name is always provided, so we can probably make it mandatory.