要将 Graphic 在地图上显示有两种方法。

  1. mapView.graphics.add(graphic)
  2. 新建一个GraphicsLayer,将graphic添加到GraphicsLayer里,然后将这个图层添加到map里面。

GraphicsLayer是什么

GraphicsLayer 是一种客户端图层,并不对应到服务端的某个地图服务,用于在客户端展现各种服务,如绘制的图形,查询返回的结果等。是保留在内存中的一种图层。

创建图层

  1. GraphicDrawLayer = new esri.layers.GraphicsLayer();
  2. Map.addLayer(GraphicDrawLayer);
  3. 或者
  4. this.gralayer = new GraphicsLayer({ id: "projectlocalgralayer" });
  5. this.map.addLayer(this.gralayer);
  6. // utils的写法
  7. /**
  8. * 创建GraphicLayer
  9. * @public
  10. */
  11. createGraphicLayer: function(id) {
  12. var layer = this.map.getLayer(id);
  13. if (!layer) {
  14. layer = new GraphicsLayer({
  15. id: id
  16. });
  17. this.map.addLayer(layer);
  18. } else {
  19. layer.clear();
  20. }
  21. return layer;
  22. }

给 GraphicsLayer 添加事件

  1. GraphicsLayer.on(
  2. "mouse-out",
  3. lang.hitch(this, function(e) {
  4. console.log("eee");
  5. })
  6. )

在 GraphicsLayer 图层中添加 Graphic 对象

  1. function drawGeo(evt) {
  2. geo = evt.geometry;
  3. var fill = new SimpleFillSymbol();
  4. var attr = {};
  5. var infotemplate = new InfoTemplate(attr);
  6. // 新建实例
  7. var graphicslayer = new GraphicsLayer();
  8. // 使用add(graphic)方法
  9. graphicslayer.add(new Graphic(geo, fill));
  10. // 使用Map的addLayer(layer)方法添加图层
  11. map.addLayer(graphicslayer);
  12. // 对graphicslayer绑定click事件
  13. graphicslayer.on("click", function(evt) {
  14. console.log(evt);
  15. }
  16. )}