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

181
Visualizações
Moving Google Maps center and marker location without refresh

Is there a way to move a marker or recenter the map without redrawing the entire map?

I am using this code

var markerLat = 39.0
var markerLon = -80.0;
var centerLat = 39.0;
var centerLon = -80.0
var myZoom = 15;

function initMap() {
  // The location of Uluru
  const uluru = { lat: centerLat, lng: centerLon };
  // The map, centered at Uluru
  const map = new google.maps.Map(document.getElementById("map"), {
    zoom: myZoom,
    center: uluru,
  });
  // The marker, positioned at Uluru
  var myLatlng = new google.maps.LatLng(markerLat,markerLon);
  const marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
  });
}

window.initMap = initMap;

let eventSource = new EventSource("/sseTest");

eventSource.onmessage = function(event) {
    console.log("new message from index.js   " + event.data);
    const myArray = event.data.split(" ");
    if(myArray[0] == 0) {
        markerLat = myArray[0];
        markerLon = myArray[1];
    } else {
        markerLat = myArray[2];
        markerLon = myArray[3];
    }

    const myMarkerLatlng = new google.maps.LatLng(markerLat, markerLon);
    const myCenterLatLon = new google.maps.LatLng(markerLat, markerLon);
    var mapOptions = {
      zoom: myZoom,
      center: myCenterLatLon,
    }
    var map = new google.maps.Map(document.getElementById("map"), mapOptions);

    var marker = new google.maps.Marker({
        position: myMarkerLatlng,
        map: map,
    });
    marker.setMap(map);
};

and it seems to redraw the map like an entire page refresh when an update (once a minute) comes in. I would like it to simply move the marker like it does when a user is sharing their location with you.

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