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

119
Visualizações
Apply changing markers array to Google map in react

Apply changing markers array to Google map in react I have a Google map in my react application which allows one single marker to be shown moving around the map (as the latitude is being changed every few seconds)

However if i had a markers array (which keeps being updated with new markers with corresponding latitude/longitude), is there a way to loop through these markers and add them to the map. It would need to keep checking they haven't changed location, or new markers haven't appeared in the array, or been deleted from the array. Essentially, it would look something like this:

    const Map = ({ location, zoomLevel, markers }) => {
        for(let marker of markers)
        {
            const [marker.markerLatVal, marker.setMarkerLatVal] = 
            useState(marker.markerLat)
        }

    useEffect(() => {
        const interval = setInterval(() => {
            changeMarkerLatitude();
        }, 2000);
        return () => clearInterval(interval);
    }, []);

    const changeMarkerLatitude = () => {
        for(let marker of markers)
        {
           marker.setMarkerLatVal(prev => prev + 50);
        }
    };
 
        return (
            <div className='map'>
                <div className='google-map'>
                    <GoogleMapReact
                     for(let marker of this.markers)
                     {
                        <MyGreatPlace lat={marker.markerLat} 
                         lng={marker.markerLong} text= 
                         {marker.markerText} />
                     }
                    </GoogleMapReact>
                </div>
            </div>
        );
 
}


 Is this possible?
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since this is non-working code, I can't really debug it, but this should get you 99.9999% of the way.

const Map = ({ location, zoomLevel, markers: markersList }) => {
    // We set a state array with the markers (renaming markersList in props to avoid collisions)
    const [markers, setMarkers] = useState(markersList)

    useEffect(() => {
        const interval = setInterval(() => {
            changeMarkerLatitude();
        }, 2000);
        return () => clearInterval(interval);
    }, []);

    const changeMarkerLatitude = () => {
        // map over each marker, keep original values by spreading old state, only modify what we want to change
        markers.map(marker => {
          setMarkers( prev => {
            ...prev,
            marker.latitude: marker.latitude += 50 //whatever you want here
          }
        }
    };
 
        return (
            <div className='map'>
                <div className='google-map'>
                    <GoogleMapReact
                     markers.map(marker => {
                        <MyGreatPlace lat={marker.markerLat} 
                        lng={marker.markerLong} text= 
                        {marker.markerText} />
                     })
                    </GoogleMapReact>
                </div>
            </div>
        );

}

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