1

我正在使用 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);
    });
4

2 回答 2

1

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

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

有关详细信息,请参阅clearMarkers

于 2013-11-12T02:18:18.397 回答
0

可能当您var marker在范围之外放置和初始化图标时,me.OpenLayers.events.register();它可能会在您再次单击新标记时重新启动。

var marker;
icon = new OpenLayers.Icon( ... );

me.OpenLayers.events.register("click", kijs_map_container, function(evt) {
    var pos = me.OpenLayers.getLonLatFromPixel(evt.xy);
    marker = new OpenLayers.Marker(pos, icon);
    markers.addMarker(marker);
});

至少我没有在这里测试过这个特定的例子,但是我正在制作地图编辑器的经验。

于 2013-11-20T17:12:56.783 回答