The Table View implemented in the Devices tab in settings on SUSI.AI Web Client has a column “geolocation” which displays the latitudinal and longitudinal coordinates of the device. These coordinates needed to be displayed on a map. Hence, we needed a Map View apart from the Table View, dedicated to displaying the devices pinpointed on a map. This blog post explains how this feature has been implemented on the SUSI.AI Web Client.
Modifying the fetched data of devices to suitable format
We already have the fetched data of devices which is being used for the Table View. We need to extract the geolocation data and store it in a different suitable format to be able to use it for the Map View. The required format is as follows:
To modify the fetched data of devices to this format, we modify the apiCall() function to facilitate extraction of the geolocation info of each device and store them in an object, namely ‘mapObj’. Also, we needed variables to store the latitude and longitude to use as the center for the map. ‘centerLat’ and ‘centerLng’ variables store the average of all the latitudes and longitudes respectively. The following code was added to the apiCall() function to facilitate all the above requirements:
The following code was added in the return function of Settings.react.js file to use the Map component. All the modified data is passed as props to this component.
The implementation of the MapContainer component is as follows:
Let us go over the code of MapContainer component step by step.
- Firstly, the componentDidUpdate() function calls the loadMap function to load the google map.
- In the loadMap() function, we first check whether props have been passed to the MapContainer component. This is done by enclosing all contents of loadMap function inside an if statement as follows:
- Then we set the prop value to google, and maps to google maps props. This is done as follows:
- Then we look for HTML div ref ‘map’ in the React DOM and name it ‘node’. This is done as follows:
- Then we set the center and the default zoom level of the map using the props we provided to the MapContainer component.
- Then we create a new Google map on the specified node (ref=’map’) with the specified configuration set above. This is done as follows:
- In the render function of the MapContainer component, we return a div with a ref ‘map’ as follows:
This is how the Map View has been implemented in the Devices tab in Settings on SUSI.AI Web Client.
- ‘google-maps-react’ library – https://www.npmjs.com/package/google-maps-react
- AJAX – https://www.w3schools.com/xml/ajax_intro.asp