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

198
Visualizações
Updating Markers to Google Maps in google-map-react

onGoogleApiLoaded not invoked on locations data change, Is there any way to achieve this?

I tried to use useEffect to load markers but then I have to save map and maps objects in useRef as well as markers

Is there any easier way to achieve this?

  import { useEffect, useRef } from 'react';
  import GoogleMapReact from 'google-map-react';

  export type SimpleMapProps = {
    locations: any[];
  };

  export default function SimpleMap({ locations }: SimpleMapProps) {
    const defaultProps =
      locations.length > 0
        ? {
            center: {
              lat: locations[0].Lat,
              lng: locations[0].Lon
            },
            zoom: 11
          }
        : {
            center: {
              lat: 0,
              lng: 0
            },
            zoom: 11
          };

    const handleApiLoadData = (map: any, maps: any) => {
      for (let i = 0; i < locations.length; i++) {
        const { Lat, Lon, hn, cur, rate } = locations[i];

        const marker = new maps.Marker({
          animation: maps.Animation.DROP,
          position: { lat: Lat, lng: Lon },
          map
        });

        marker.customInfowindow = new maps.InfoWindow({
          content: `<div>${hn} </br> (${cur} ${rate})</div>`
        });

        marker.addListener('click', () => {
          marker.customInfowindow.open(map, marker);
        });
      }
    };

    return (
      // Important! Always set the container height explicitly
      <div style={{ height: '50vh', width: '100%' }}>
        <GoogleMapReact
          bootstrapURLKeys={{
            key: 'XXXXXXXXXXXXXXX'
          }}
          center={defaultProps.center}
          zoom={defaultProps.zoom}
          yesIWantToUseGoogleMapApiInternals
          onGoogleApiLoaded={({ map, maps }) => handleApiLoadData(map, maps)}
        />
      </div>
    );
  }
about 4 years ago · Juan Pablo Isaza
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