[OpenLayers-Users] Problem with removeMarker

Huub Fleuren huub.fleuren at geodan.nl
Sun Mar 16 06:25:29 EDT 2008



Maybe, try working downward from markers.length, down to 0;

regards, Huub

Linda Rawson wrote:
> I have the following code:
>
> <html xmlns="http://www.w3.org/1999/xhtml">
>   <head>
>     <style type="text/css">
>         #map {
>             width: 512px;
>             height: 512px;
>             border: 1px solid black;
>         }
>     </style>
>     <script src="http://www.openlayers.org/dev/lib/OpenLayers.js"></script>
>     <script type="text/javascript">
>         var map, layer, markerLayer;
>
>         function init(){
>             map = new OpenLayers.Map('map');
>             layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
>                 "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
>
>             map.addLayer(layer);
>             markerLayer = new OpenLayers.Layer.Markers( "Markers" );
>             map.addLayer(markerLayer);
>
>             var size = new OpenLayers.Size(10,17);
>             var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
>             var icon = new
> OpenLayers.Icon('http://boston.openguides.org/markers/AQUA.png',size,offset);
>             markerLayer.addMarker(new OpenLayers.Marker(new
> OpenLayers.LonLat(0,0),icon));
>             markerLayer.addMarker(new OpenLayers.Marker(new
> OpenLayers.LonLat(30,30),icon.clone()));
>             markerLayer.addMarker(new OpenLayers.Marker(new
> OpenLayers.LonLat(45,45),icon.clone()));
>             markerLayer.addMarker(new OpenLayers.Marker(new
> OpenLayers.LonLat(60,45),icon.clone()));
>             markerLayer.addMarker(new OpenLayers.Marker(new
> OpenLayers.LonLat(90,45),icon.clone()));
>             markerLayer.addMarker(new OpenLayers.Marker(new
> OpenLayers.LonLat(105,45),icon.clone()));
>             markerLayer.addMarker(new OpenLayers.Marker(new
> OpenLayers.LonLat(120,30),icon.clone()));
>             map.zoomToMaxExtent();
>         }
>         function deleteMarkers() {
>             var myMarkers = markerLayer.markers;
>             for (mrkCnt = 0; mrkCnt < myMarkers.length; mrkCnt++) {
>         	var myMarker = myMarkers[mrkCnt];
>                     if (myMarker.lonlat.lat == 45) {
>             	      markerLayer.removeMarker(myMarker);
> 	}
>              }
>         }
>     </script>
>   </head>
>   <body onload="init()">
>     <h1>OpenLayers Example</h1>
> 	<input type="button" value="Delete Markers with 45" onClick="deleteMarkers();">
>     <div id="map"></div>
>   </body>
> </html>
>
> It simply adds 7 markers to the map.  Then I have a button that should
> remove 4 of them leaving me with 3.
>
> It only deletes 2 of the markers and somehow resets the length on the
> markers array so that it does not cycle through the rest of the
> markers to find the additional markers to delete.
>
> I need the ability to add the same icon over and over what and then
> delete it.  What am I doing wrong?
>
>   




More information about the Users mailing list