[OpenLayers-Dev] delete a marker when i redraw an other marker

Eric Lemoine eric.c2c at gmail.com
Sat Feb 23 12:51:02 EST 2008


Hi Stéphane,

See my comments inline.


On Thu, Feb 21, 2008 at 5:20 PM, steve1000 <ligerstephane at free.fr> wrote:
>
>  All is in the title, i would want to erase the marker when i redraw an other
>  marker.
>
>  My Code:
>  map.events.register("click", map, function(e,marker) {
>
>                 //markers.removeMarker();my code who is the problem
>                 //markers.clearMarkers();my code who is the problem

clearMarkers() is indeed the method to call to remove all markers from
a markers layer. The removeMarker() takes a marker as an argument, it
has no effect if called with no argument.

>
>                 var lonlat = map.getLonLatFromViewPortPx(e.xy);
>
>                 document.getElementById('Nord').value = lonlat.lat;
>                 document.getElementById('Est').value = lonlat.lon;
>
>                 var markers = new OpenLayers.Layer.Markers( "Markers" );

I don't understand why you create a new markers layer on each click.
Instead, I think you'd be better off creating one markers layer
outside the click handler and use that layer for adding a marker on
each click.

>
>                 map.addLayer(markers);
>
>                 var size = new OpenLayers.Size(10,17);
>                 var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
>                 var icon = new OpenLayers.Icon('../img/marker.png',size,offset);
>
>
>                 markers.addMarker(new OpenLayers.Marker(new
>  OpenLayers.LonLat(lonlat.lon,lonlat.lat),icon));
>                 markers.addMarker(new OpenLayers.Marker(new
>  OpenLayers.LonLat,icon.clone(lonlat.lon,lonlat.lat)));

Why would you add two markers?

>
>                 });
>
>
>  I am no example for the use of this function : markers.removeMarker(); and
>  markers.clearMarkers(); so if you think that you would to help me please
>  write me.

As I said, you clearMarkers() to remove all markers of a markers layer.

Cheers,
--
Eric


More information about the Dev mailing list