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);

google maps javascript api v3 -- create Radius from center position --

            var image = 'aircraftsmall.png';
            var marker = new google.maps.Marker // Set the marker
            ({
                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
            });
            circle = new google.maps.Circle
            ({
                fillColor: '#00f',
                fillOpacity: 0.1,
                strokeColor: '#00f',
                strokeOpacity: 0.1,
                strokeweight:1
            });
            circle.bindTo('center', marker, 'position');
            circle.setRadius(150);
            circle.setMap(map);