I'm running a simple react-lefleat map on small http server running on an Arduino board and I would like to update a marker's position (my position) according to the coordinates extraced by the Arduino from a connected GPS board.
The whole thing should work like this:
- the Arduino takes the coordinates from the GPS and writes latidude and longitude in a .env file;
- the React-Leaflet App should update the markers's position every 1 or 2 seconds I use an .env file because when I run the build command (npm run build) the .env file remains outside the minimized .js files and thus it is easly accesible (and writable) from the Arduino side (the App stays on a SD card). I did use the setInterval method but works it only for six times (that is the fetching's output). Does someone have any idea on how to update only the component that renders the marker, let's say every 1 o 2 second? The component's code is this:
import React, { useState, useEffect } from "react";
import textfile from "./data.env";
import { Marker, Popup } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import L from 'leaflet';
import icon from 'leaflet/dist/images/marker-icon.png';
import iconShadow from 'leaflet/dist/images/marker-shadow.png';
//import ReadTxt from "./ReadTxt";
//import LetturaTxt from "./LetturaTxt";
let DefaultIcon = L.icon({
iconUrl: icon,
shadowUrl: iconShadow
});
L.Marker.prototype.options.icon = DefaultIcon;
function Interval() {
const [text, setText] = useState(0);
useEffect(() => {
//Implementing the setInterval method
const interval = setInterval(() => {
fetch(textfile)
.then((response) => response.text())
.then((textContent) => {
setText(textContent);
}, []);
}, 1000);
//Clearing the interval
return () => clearInterval(interval);
}, []);
//var result = parseFloat(text);
//console.log(text)
if (text > 0) {
console.log(text)
return (
<Marker position={[parseFloat(text), +11.000]}>
<Popup>
A pretty CSS3 popup. <br /> Easily customizable.
</Popup>
</Marker>
)
}
};
export default Interval;
while the main code of the App is this:
//import React from "react";
import React, { useRef } from "react";
import { MapContainer, TileLayer, GeoJSON } from "react-leaflet";
import "leaflet/dist/leaflet.css";
//import Marcatore from "./Marcatore";
import { HashRouter } from "react-router-dom";
//import LetturaTxt from "./LetturaTxt";
import CTR from "./Provincie.json";
//import Interval2 from "./Interval2";
//import ImportJS from "./ImportJS";
import Interval from "./Interval";
function App() {
const latitude = 43.000;
const longitude = 11.000;
const mapRef = useRef(null);
return (
<HashRouter>
<MapContainer center={[latitude, longitude]} zoom={13} ref={mapRef} style={{ height: "99vh", width: "vw" }}>
<div>
<Interval />
</div>
<GeoJSON data={CTR} />
</MapContainer>
</HashRouter >
);
}
export default App;
Thanks a lot in advance! Sebastian
I finally found the solution, here is the code for the component:
The solution stands in particular in this part of code:
and where the Fetch() component is this: