Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda