Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/HEREMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
*/
engineType?: H.Map.EngineType,
showActiveAndInactiveTruckRestrictions?: boolean,
/**
* @default false
*/
hideTruckRestrictionsWhenZooming?: boolean,
trafficLayer?: boolean,
useSatellite?: boolean,
disableMapSettings?: boolean,
Expand Down Expand Up @@ -88,12 +92,13 @@
congestion,
truckRestrictions,
showActiveAndInactiveTruckRestrictions,
hideTruckRestrictionsWhenZooming,
apiKey,
animateZoom,
animateCenter,
useVectorTiles,
engineType = H.Map.EngineType.HARP,
}, ref) => {

Check warning on line 101 in src/HEREMap.tsx

View workflow job for this annotation

GitHub Actions / lint

Refactor this function to reduce its Cognitive Complexity from 43 to the 15 allowed
if (engineType === H.Map.EngineType.WEBGL) {
throw new Error('WEBGL Engine is not supported.')
}
Expand Down Expand Up @@ -133,6 +138,7 @@
useSatellite,
enableRasterLayers: !useVectorTiles && engineType === H.Map.EngineType.HARP,
hidpi,
hideTruckRestrictionsWhenZooming,
})

useLegacyRasterLayers({
Expand Down Expand Up @@ -218,7 +224,7 @@
zoomOnMarkers,
zoomOnMarkersSet,
}
}, [map])

Check warning on line 227 in src/HEREMap.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useImperativeHandle has missing dependencies: 'addToMarkerGroup', 'animateCenter', 'animateZoom', 'removeFromMarkerGroup', 'screenToGeo', 'zoomOnMarkers', and 'zoomOnMarkersSet'. Either include them or remove the dependency array

useEffect(() => {
if (unmountedRef.current) {
Expand Down Expand Up @@ -292,7 +298,7 @@
unmountedRef.current = true
map?.dispose()
}
}, [])

Check warning on line 301 in src/HEREMap.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has missing dependencies: 'apiKey', 'center', 'disableMapSettings', 'engineType', 'hidpi', 'interactive', 'language', 'map', 'onMapAvailable', 'trafficLayer', 'useVectorTiles', and 'zoom'. Either include them or remove the dependency array. If 'onMapAvailable' changes too often, find the parent component that defines it and wrap that definition in useCallback

useEffect(() => {
if (map) {
Expand Down
38 changes: 34 additions & 4 deletions src/useRasterLayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface UseRasterLayersProps {
enableRasterLayers: boolean,
language: string,
hidpi?: boolean,
/**
* @default false
*/
hideTruckRestrictionsWhenZooming?: boolean,
}

const getBaseLayer = ({
Expand Down Expand Up @@ -90,6 +94,7 @@ export const useRasterLayers = ({
enableRasterLayers,
showActiveAndInactiveTruckRestrictions,
hidpi,
hideTruckRestrictionsWhenZooming,
}: UseRasterLayersProps) => {
const truckOverlayLayer = useMemo(() => map && getTruckOverlayLayer({
apiKey,
Expand All @@ -116,18 +121,43 @@ export const useRasterLayers = ({
}, [map, useSatellite, defaultLayers, baseLayer, enableRasterLayers])

useEffect(() => {
if (!map || !enableRasterLayers || !truckOverlayLayer) {
if (!map || !enableRasterLayers || !truckOverlayLayer || !truckRestrictions) {
return
}

if (truckRestrictions) {
map.addLayer(truckOverlayLayer)
const syncEventListener = (e: H.map.ChangeEvent) => {
if (e.oldValue.lookAt.zoom !== e.newValue.lookAt.zoom) {
map.removeLayer(truckOverlayLayer)
}
}

const mapViewChangeEndEventListener = () => {
const dataModelLayers = map.getLayers().asArray()
if (dataModelLayers.indexOf(truckOverlayLayer) === -1) {
map.getLayers().add(truckOverlayLayer)
}
}

if (hideTruckRestrictionsWhenZooming) {
// Listen for changes in the view model, i.e. position, zoom level
// Remove the overlay only if the zoom level changes, i.e. during the zoomin/out operation
// In fact, we want the overlay to stay visible during panning operations
map.getViewModel().addEventListener('sync', syncEventListener)

// Listen for the mapviewchangeend event.
// We want to re-add the overlay at the end of the interaction with the map, either panning or zoomin/out.
// Specifically, we re-add the overlay only if the layer is not already present in the layers stack
map.addEventListener('mapviewchangeend', mapViewChangeEndEventListener)
}

map.addLayer(truckOverlayLayer)

return () => {
map.removeLayer(truckOverlayLayer)
map.getViewModel().removeEventListener('sync', syncEventListener)
map.removeEventListener('mapviewchangeend', mapViewChangeEndEventListener)
}
}, [truckRestrictions, map, enableRasterLayers, truckOverlayLayer])
}, [truckRestrictions, map, enableRasterLayers, truckOverlayLayer, hideTruckRestrictionsWhenZooming])

useEffect(() => {
if (!map || !defaultLayers || !enableRasterLayers) {
Expand Down
Loading