Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

474
Visualizações
Changing React Leaflet Circle color with Leaflet Timedimension Plugin

I'm building a React app that has a Leaflet map and integrates the Leaflet Timedimension Plugin.
The goal is to have a map that has some Circles, those changes radius and color whenever the Timedimension plugin loads a new time.

I've successfully created a map that has some Circles that changes based on the Timedimension plugin times (currentTimeIndex changes whenever a new time is loaded), but the colour wouldn't change.
The following code it's my approach to the problem:

      data.map((region) =>
        region.items.map((market) => (
          <Circle
            center={[market.lat, market.lon]}
            radius={values[currentTimeIndex]}
            pane='markets'
            key={market.id}
            color={colors[currentTimeIndex]}
          />
        )),
      )

Is possible to change the color of the Circle without having to remove and re-add them?

I'll leave here the codesandbox with the full code.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

For weird reasons I cannot really explain, it seems like colors does not update, while pathOptions seems to work as intended.

I've slightly updated your codesandbox which seems to produce the desired result, the following were changed:

  • I have moved colors out of the Leaflet component since it's actually a constant.
  • I have added a useMemo hook to get the current color according to the currentTimeIndex.
  • I have changed color with pathOptions, which is the real fix you need to do.

Side note: I'm not sure how you want to style the circle itself. In case you want to use both fill and stroke, use fill and color in pathOptions, you can find documentation about that in the typings: https://definitelytyped.org/docs/leaflet--leaflet/interfaces/l.pathoptions.html

Relevant code:

const colors = ["red", "green", "blue", "black", "purple", "gray"];
const Leaflet = () => {
  const map = useMap();
  const [currentTimeIndex, setCurrentTimeIndex] = useState(0);
  const values = [5, 15, 25, 5, 15, 25, 5];

  map.timeDimension.on("timeloading", (data) => {
    console.log(data.target._currentTimeIndex);
    setCurrentTimeIndex(data.target._currentTimeIndex);
  });

  const currentColor = useMemo(() => colors[currentTimeIndex], [
    currentTimeIndex
  ]);

  return (
    <>
      <Pane name="markets" />
      <TileLayer url={"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"} />
      {SenegalData.map((region) =>
        region.items.map((market) => (
          <Circle
            center={[market.lat, market.lon]}
            radius={values[currentTimeIndex] * 1000}
            pane="markets"
            key={market.id}
            pathOptions={{
              color: currentColor
            }}
          />
        ))
      )}
    </>
  );
};

Step 0 (color red): step 0

Step 1 (color green): step 1

Updated codesandbox: https://codesandbox.io/s/upbeat-dew-x11s1?file=/src/Leaflet.js

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda