How to stay in my last position on the map after every 1 min of load

how to track the current position on the map when I load the map every 1 min, but right now the map position is redirected to the default position every 1 min.

here is my code.i code upload function every 1 minute and i am extracting data from mapajax1.php file. I want to stay in the last position on the map because I track the driver

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"></script>
    <script type="text/javascript">
     function load() {

          var map = new google.maps.Map(document.getElementById("map"), {
            center: new google.maps.LatLng("<?php echo $lat;?>", "<?php echo $lng;?>"),
            zoom: 5,
            mapTypeId: 'roadmap'
          });
          var infoWindow = new google.maps.InfoWindow;

          // Change this depending on the name of your PHP file
          downloadUrl("mapajax1.php", function(data) {
            var xml = data.responseXML;


            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
              var name = markers[i].getAttribute("name");
              var email = markers[i].getAttribute("email");
              var phone = markers[i].getAttribute("phone");
              var status = markers[i].getAttribute("status");
              var type = markers[i].getAttribute("type");
              var point = new google.maps.LatLng(
                  parseFloat(markers[i].getAttribute("lat")),
                  parseFloat(markers[i].getAttribute("lng")));
              var html = "<b>" + name + "</b> <br/>" + email+ "</b> <br/>" + phone+ "</b> <br/>" + status;
              var icon = customIcons[type] || {};
              var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon.icon
              });
              bindInfoWindow(marker, map, infoWindow, html);
            }
          });
        }

     setInterval(function(){ 
                load()    
            }, 60000);
    </script>
+4
source share
2 answers

Before calling the load () function, you need to save the last position on the map. because of this, your code returns to its original position.

, 5 .

jsfiddle

var lat = -27.4; //<?php echo $lat;?>
var lng = 153.023; //<?php echo $lng;?>

function load() {

  var map = new google.maps.Map(document.getElementById("map"), {
    center: new google.maps.LatLng(lat, lng),
    zoom: 5,
    mapTypeId: 'roadmap'
  });
  //....... rest of code
  map.addListener('center_changed', function() {
    lat = map.getCenter().lat();
    lng = map.getCenter().lng();
  });
}

setInterval(function() {
  load()
}, 5000);

lat lng

  map.addListener('center_changed', function() {
    lat = map.getCenter().lat();
    lng = map.getCenter().lng();
  });
0

. , ? HTML5 . , IP-. . . , .

var lat = null;
var lng = null;

// sets your location as default
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    var locationMarker = null;
    if (locationMarker){
      // return if there is a locationMarker bug
      return;
    }

    lat = position.coords["latitude"];
    lng = position.coords["longitude"];

   console.log(lat, lng);

  },
  function(error) {
    console.log("Error: ", error);
  },
  {
    enableHighAccuracy: true
  }
  );
}
0

Source: https://habr.com/ru/post/1654772/