I am trying to add an array of randomly placed markers to a Google Map in Flash with the API V3 and AS3.
I can create the markers no problem, but I'm having a bit of trouble putting them into an array and then clearing them afterwards as I need to provide this functionality.
I saw another post where people were talking about the lack of map.clearOverlays(); in V3? and I need a bit of help incorporated my code .
var markers:Array = new Array();
function addmarkers()
{
// Add 10 markers to the map at random locations
var bounds:LatLngBounds = map.getLatLngBounds();
v开发者_StackOverflowar southWest:LatLng = bounds.getSouthWest();
var northEast:LatLng = bounds.getNorthEast();
var lngSpan:Number = northEast.lng() - southWest.lng();
var latSpan:Number = northEast.lat() - southWest.lat();
for (var i:int = 0; i < 10; i++)
{
var newLat:Number = southWest.lat() + (latSpan * Math.random());
var newLng:Number = southWest.lng() + (lngSpan * Math.random());
var latlng:LatLng = new LatLng(newLat,newLng);
map.addOverlay(new Marker(latlng));
markers.push();
}
}
I've got a button that calls this but then I also want a button that
can you loop through the array and call map.removeOverlay() on each marker?
function removeMarkers():void {
while(markers.length > 0) {
var m:Marker = markers.shift() as Marker;
map.removeOverlay(m);
m = null;
}
}
my working code with yours incorporated, thanks.
function dorandomPoints(e:MouseEvent):void
{
if (chkbox.selected)
{
var bounds:LatLngBounds = map.getLatLngBounds();
var southWest:LatLng = bounds.getSouthWest();
var northEast:LatLng = bounds.getNorthEast();
var lngSpan:Number = northEast.lng() - southWest.lng();
var latSpan:Number = northEast.lat() - southWest.lat();
for (var i:int = 0; i < 10; i++)
{
var newLat:Number = southWest.lat() + (latSpan * Math.random());
var newLng:Number = southWest.lng() + (lngSpan * Math.random());
var latlng:LatLng = new LatLng(newLat,newLng);
var marker:Marker = new Marker(latlng);
markerArray.push(marker);
map.addOverlay(markerArray[i]);
//map.removeOverlay(marker);
//trace(markerArray.length);
}
}
else
{
while (markerArray.length > 0)
{
var m:Marker = markerArray.shift() as Marker;
map.removeOverlay(m);
m = null;
}
}
}
精彩评论