【问题标题】:How to drop a pin from variables in JavaScript?如何从 JavaScript 中的变量中删除 pin?
【发布时间】:2020-07-11 16:37:13
【问题描述】:

我想使用保存在 JavaScript 变量 x 和 y 中的数据来放置图钉。我怎样才能做到这一点?下面是放置图钉并获取坐标的代码,有没有办法可以更改它,以便使用保存在变量中的数据删除图钉?

<script>
      require([
        "esri/widgets/Sketch",
        "esri/Map",
        "esri/layers/GraphicsLayer",
        "esri/views/MapView"
      ], function(Sketch, Map, GraphicsLayer, MapView) {
        const layer = new GraphicsLayer();

        const map = new Map({
          basemap: "streets",
          layers: [layer]
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 5,
          center: [90, 45]
        });
		
		var symbol = {
                    type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
                    style: "circle",
                    color: "blue",
                    size: "8px",  // pixels
                    outline: {  // autocasts as new SimpleLineSymbol()
                    color: [ 255, 255, 0 ],
                    width: 1  // points
                    }
                };

        const sketch = new Sketch({
          layer: layer,
          view: view,
		  symbol: symbol,
          availableCreateTools: ["point"]
        });

        view.ui.add(sketch, "top-right");
        
        sketch.on('create', function (evt) {
        if (view.zoom >= 11) {
			// check if the create event's state has changed to complete indicating
            // the graphic create operation is completed.
          let gra = evt.graphic.clone();
          evt.graphic.layer.removeAll();
          gra.symbol.color = "blue";
          layer.add(gra);
          console.log("X = ", gra.geometry.x);
          console.log("Y = ", gra.geometry.y);
        } else {
          alert("please zoom in");
          evt.graphic.layer.remove(evt.graphic);
        }
      });
      });
    </script>

请帮忙。

【问题讨论】:

  • 如果我理解正确,我现在不明白,你想从图形层中删除一个图形吗? ..你能举一个关于保存在变量中的数据的例子”,你的意思是什么?

标签: javascript esri arcgis-js-api


【解决方案1】:

如果我理解这个问题,您想使用 X 和 Y 值向地图添加一个点。

有一个教程 - https://developers.arcgis.com/javascript/latest/guide/display-point-line-and-polygon-graphics/

简化该教程/示例以展示如何基于一个点(使用 X 和 y)和一个符号创建图形 - https://codepen.io/bsvensson/pen/GRJeMeR

  // Create a point
  var point = {
    type: "point",
    longitude: -118.80657,
    latitude: 34.00059
  };
  var simpleMarkerSymbol = {
    type: "simple-marker",
    color: [226, 119, 40], // orange
    size: 30,
    style: "triangle", // default is a circle
    outline: {
      color: [255, 255, 255], // white
      width: 2
    }
  };
  var pointGraphic = new Graphic({
    geometry: point,
    symbol: simpleMarkerSymbol
  });
  graphicsLayer.add(pointGraphic);

【讨论】:

  • 谢谢 Bjorn Svensson,让我试试这个。
  • 嗨 Bjorne Svensson,我测试了您推荐的代码,它 100% 有效。但是现在我想修改我的代码以使其与该代码对齐,当我运行它时它不显示地图,请您告知我做错了什么。我已将我的最新代码添加到原始帖子中。
猜你喜欢
  • 2012-12-07
  • 2010-12-29
  • 1970-01-01
  • 2015-07-09
  • 2011-01-16
  • 1970-01-01
  • 2013-01-04
  • 2022-06-28
  • 1970-01-01
相关资源
最近更新 更多