@@ -5,7 +5,7 @@ import 'mapbox-gl/dist/mapbox-gl.css';
55import { MAPBOX_TOKEN } from './config.js' ;
66import ColorHash from 'color-hash'
77
8- function Mapbox ( { selectedVehicle, vehicles, mapPanelSize, tracking } ) {
8+ function Mapbox ( { selectedVehicle, vehicles, mapPanelSize, tracking, detectedObjects , mapHeight } ) {
99 const mapRef = useRef ( )
1010 const mapContainerRef = useRef ( )
1111 const [ currentLoc , setCurrentLoc ] = useState ( null ) ;
@@ -78,7 +78,7 @@ function Mapbox({ selectedVehicle, vehicles, mapPanelSize, tracking }) {
7878 vehicles . forEach ( v => {
7979 let marker = new mapboxgl . Marker ( { "color" : colorHash . hex ( v . name ) , rotation : v . bearing , rotationAlignment : 'map' } )
8080 . setLngLat ( [ v . current . long , v . current . lat ] )
81- . setPopup ( new mapboxgl . Popup ( { focusAfterOpen : false } ) . setHTML ( `<strong style="color:black">${ v . name } (${ v . current . alt . toFixed ( 2 ) } m)</strong>` ) ) // add popup
81+ . setPopup ( new mapboxgl . Popup ( { focusAfterOpen : false } ) . setHTML ( `<strong style="color:black">${ v . name } (${ v . current . alt . toFixed ( 2 ) } m)</strong>` ) )
8282 . addTo ( mapRef . current ) ;
8383 marker . togglePopup ( ) ;
8484 const markerDiv = marker . getElement ( ) ;
@@ -96,7 +96,37 @@ function Mapbox({ selectedVehicle, vehicles, mapPanelSize, tracking }) {
9696 markerRefs . current . push ( marker ) ;
9797 } ) ;
9898
99- } , [ vehicles ] ) ;
99+ if ( detectedObjects != null ) {
100+ detectedObjects . forEach ( d => {
101+ // Create the SVG element
102+ const el = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'svg' ) ;
103+ el . setAttribute ( 'width' , '16' ) ;
104+ el . setAttribute ( 'height' , '16' ) ;
105+ el . setAttribute ( 'viewBox' , '0 0 16 16' ) ;
106+
107+ const circle = document . createElementNS ( 'http://www.w3.org/2000/svg' , 'circle' ) ;
108+ circle . setAttribute ( 'cx' , '8' ) ;
109+ circle . setAttribute ( 'cy' , '8' ) ;
110+ circle . setAttribute ( 'r' , '7' ) ;
111+ circle . setAttribute ( 'fill' , colorHash . hex ( d . cls ) ) ;
112+ circle . setAttribute ( 'stroke' , '#fff' ) ;
113+ circle . setAttribute ( 'stroke-width' , '2' ) ;
114+
115+ el . appendChild ( circle ) ;
116+
117+ let marker = new mapboxgl . Marker ( { element : el } )
118+ . setLngLat ( [ d . longitude , d . latitude ] )
119+ . setPopup ( new mapboxgl . Popup ( { focusAfterOpen : false } ) . setHTML ( `<strong style="color:black">${ d . id } (${ d . confidence . toFixed ( 2 ) * 100 } %)</strong><img src="${ d . link } " style="width:100%;height:auto;margin-top:5px;" />` ) )
120+ . addTo ( mapRef . current ) ;
121+ const markerDiv = marker . getElement ( ) ;
122+
123+ markerDiv . addEventListener ( 'mouseenter' , ( ) => marker . togglePopup ( ) ) ;
124+ markerDiv . addEventListener ( 'mouseleave' , ( ) => marker . togglePopup ( ) ) ;
125+ markerRefs . current . push ( marker ) ;
126+ } ) ;
127+ }
128+
129+ } , [ vehicles , detectedObjects ] ) ;
100130
101131 useEffect ( ( ) => {
102132 let v = vehicles . find ( v => v . name === selectedVehicle ) ;
@@ -112,7 +142,7 @@ function Mapbox({ selectedVehicle, vehicles, mapPanelSize, tracking }) {
112142 } , [ selectedVehicle ] ) ;
113143
114144 return (
115- < div id = 'map-container' ref = { mapContainerRef } style = { { width : '100%' , height : '20rem' } } />
145+ < div id = 'map-container' ref = { mapContainerRef } style = { { width : '100%' , height : mapHeight || '20rem' } } />
116146 )
117147}
118148
0 commit comments