0

如何使用 javascript api 在 esri 地图上添加和删除动态标记?当我在图形图层中添加标记时,它已添加,但我如何删除它并通过另一个纬度添加新标记?

到目前为止,这是我的代码;

require(
    ["esri/map",
    "esri/graphic",
    "esri/symbols/PictureMarkerSymbol",
    "esri/symbols/TextSymbol",
    "esri/geometry/Point",
    "esri/SpatialReference",
    "esri/tasks/ProjectParameters",
    "esri/tasks/GeometryService",
    "dojo/dom",
    "dojo/on",
    "esri/dijit/HomeButton",
    "dojo/domReady!"
    ],
    function setupmap(Map, Graphic, PictureMarkerSymbol, TextSymbol, Point, SpatialReference, ProjectParameters, GeometryService, dom, on, HomeButton) {
        var map = new Map("map-container", {
            center: [83.0179802, 25.32327],
            zoom: 13,
            basemap: "streets"
        });
        map.graphics.clear();
        map.on("load", function (evt) {
        var home = new HomeButton({map: map}, "HomeButton");
        home.startup();
        picSymbol = new PictureMarkerSymbol(iconType, 20, 20);
        $.each(detailsJSON, function (location, lstNodes) {
            var locArr = location.split("--");
            var latitude=locArr[0];
            var longitude=locArr[1];
            var geometryPoint = new Point(longitude, latitude,new SpatialReference(4326));
            map.graphics.add(new Graphic(geometryPoint, picSymbol));
        });
        });
    }
);
4

2 回答 2

1

您可以存储对要添加的 Graphic 对象的引用,然后使用该remove(graphic)方法将其删除。

let graphic = new Graphic(geometryPoint, picSymbol);
map.graphics.add(graphic);
...
map.graphics.remove(graphic);

您还可以使用方法从图层中删除所有图形removeAll()

有关详细信息,请参阅arcgis-js-api 参考

为了使您的组件更加无状态,您可以使用 的attributes集合Graphic来存储标签(或 Id 或类似物)并根据此值删除项目。

添加时;

let graphic = new Graphic(geometryPoint, picSymbol);
graphic.attributes = { "tag": "toBeRemovedLater" };
map.graphics.add(graphic);

拆除时;

angular.forEach(map.graphics.graphics, (graphic: any) => {
   if (graphic.attributes && graphic.attributes.tag == "toBeRemovedLater")
       map.graphics.remove(graphic);
   });
于 2020-04-07T10:53:39.030 回答
1

您可以使用 Sketch Widget 来简化添加和更新图形的过程。

const sketch = new Sketch({ availableCreateTools: ['point'], layer: graphicsLayer, view, }); view.ui.add(sketch, 'top-right');

于 2020-04-09T11:14:30.443 回答