在鼠标点击上添加一个标记并删除Open Layers中现有的On?

2022-04-05 00:00:00 marker javascript maps openlayers

我使用的是OpenLayers贴图。我想要一个功能,当用户单击地图时,应该创建标记,但同时应该删除或移除地图上已经存在的现有标记,并且应该只显示最新的标记。

  var markers = new OpenLayers.Layer.Markers( "Markers" );
    markers.id = "Markers";
    me.OpenLayers.addLayer(markers);
    /*myMarker = new OpenLayers.Marker(new OpenLayers.Marker( 56.512438257836,27.335700987698 ));
     markers.addMarker(myMarker);*/

    var size = new OpenLayers.Size(30,30);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png',size,offset);

    //map.setCenter (lonLat, zoom);
    me.OpenLayers.events.register("click", kijs_map_container, function(evt) {
        var lonlat = me.OpenLayers.getLonLatFromViewPortPx(evt.xy).transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
        $("#edit-field-jena-seta-map-openlayers-wkt").val('GEOMETRYCOLLECTION(POINT('+lonlat.lat+'  '+lonlat.lon+'))');
        var pos = me.OpenLayers.getLonLatFromPixel(evt.xy);
        alert(baltic_long);
        var marker = new OpenLayers.Marker(new OpenLayers.LonLat(baltic_lat, baltic_long),icon);
        markers.addMarker(marker);
        marker.events.register("click", marker, function(e){

        });
        //updateMaker(myMarker, pos);
    });

解决方案

在创建和添加新标记之前删除层上的所有标记:

markers.clearMarkers(); 
markers.addMarker(marker);

有关详细信息,请参阅clearMarkers。

相关文章