Monday, August 31, 2015

google maps javascript api v3 -- remove last marker for updated gps position --

In the global scope:
var markers = [];
 
 
Then push the markers on that array as you create them:
      

         var myLatlng = new google.maps.LatLng(lat, lng);
                var image = 'aircraftsmall.png';
                var marker = new google.maps.Marker // Set the marker
                ({
                    position: myLatlng, // Position marker to coordinates
                    //position: new google.maps.LatLng(lat, lng),
                    icon: image, //use our image as the marker
                    map: map, // assign the marker to our map variable
                    title: 'LPN-01' // Marker ALT Text
                });
                markers.push(marker);


 Then to remove them:

markers[markers.length-2].setMap(null);


Completed code:

         var myLatlng = new google.maps.LatLng(lat, lng);
                var image = 'aircraftsmall.png';
                var marker = new google.maps.Marker // Set the marker
                ({
                    position: myLatlng, // Position marker to coordinates
                    //position: new google.maps.LatLng(lat, lng),
                    icon: image, //use our image as the marker
                    map: map, // assign the marker to our map variable
                    title: 'LPN-01' // Marker ALT Text
                });
                markers.push(marker);
                markers[markers.length-2].setMap(null);

No comments:

Post a Comment