简体   繁体   中英

refreshing marker position every 5 seconds in google maps JavaScript API

      setInterval(function () {initMap();},5000);
       //setInterval(function () {changeMarkerPosition(marker);},5000);
          var map;

          function initMap() {
            var lat = document.getElementById( 'lat' ).value;
            var lng = document.getElementById( 'lng' ).value;

            map = new google.maps.Map(
                document.getElementById('map'),
                {center: new google.maps.LatLng(lat, lng), zoom: 16});

            var iconBase =
                'https://developers.google.com/maps/documentation/javascript/examples/full/images/';

            var icons = {
              parking: {
                icon: iconBase + 'parking_lot_maps.png'
              },
              library: {
                icon: iconBase + 'library_maps.png'
              },
              info: {
                icon: iconBase + 'info-i_maps.png'
              }
            };

            var features = [
              {
                position: new google.maps.LatLng(lat, lng),
                type: 'info'
              },
            ];

            // Create markers in the map.
            for (var i = 0; i < features.length; i++) {
              var marker = new google.maps.Marker({
                position: features[i].position,
                icon: icons[features[i].type].icon,
                map: map
              });
            };
          }

This is my code which refreshes the whole map every 5 seconds instead i want to refresh the marker position every 5 seconds as i am getting the latitude and longitude from two input fields which are updating continuously

Please note that every time you call google.maps.Map() class, you are creating one Map Load which is billed as Dynamic Maps SKU . Instead of creating a map load every 5 seconds just to update the marker position, you can leverage the setPosition method of the google.maps.Marker class .

Here is a sample code where I am changing the markers position using the setPosition every 5 seconds.

Here is the breakdown of the code:

This is where I set the marker's initial position

 marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        title: 'Hello World!'
    });

This is the array of my coordinates where my markers should change position to:

    markerList = [
        [-33.950198, 151.259302],
        [-33.923036, 151.259052],
        [-34.028249, 151.157507],
        [-33.80010128657071, 151.28747820854187]
    ];

I then used a for loop to go through all the coordinates in the array. Then put the setTimeout to put 5 seconds interval when changing the marker position:

for (var i = 0; i < markerList.length; i++) {
        (function(index) {
            setTimeout(function() {
                marker.setPosition(new google.maps.LatLng(markerList[index][0], markerList[index][1]));
            }, i * 5000);
        })(i);
    }

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM