要将 Graphic 在地图上显示有两种方法。
- mapView.graphics.add(graphic)
- 新建一个GraphicsLayer,将graphic添加到GraphicsLayer里,然后将这个图层添加到map里面。
GraphicsLayer是什么
GraphicsLayer
是一种客户端图层,并不对应到服务端的某个地图服务,用于在客户端展现各种服务,如绘制的图形,查询返回的结果等。是保留在内存中的一种图层。
创建图层
GraphicDrawLayer = new esri.layers.GraphicsLayer();
Map.addLayer(GraphicDrawLayer);
或者
this.gralayer = new GraphicsLayer({ id: "projectlocalgralayer" });
this.map.addLayer(this.gralayer);
// utils的写法
/**
* 创建GraphicLayer
* @public
*/
createGraphicLayer: function(id) {
var layer = this.map.getLayer(id);
if (!layer) {
layer = new GraphicsLayer({
id: id
});
this.map.addLayer(layer);
} else {
layer.clear();
}
return layer;
}
给 GraphicsLayer 添加事件
GraphicsLayer.on(
"mouse-out",
lang.hitch(this, function(e) {
console.log("eee");
})
)
在 GraphicsLayer 图层中添加 Graphic 对象
function drawGeo(evt) {
geo = evt.geometry;
var fill = new SimpleFillSymbol();
var attr = {};
var infotemplate = new InfoTemplate(attr);
// 新建实例
var graphicslayer = new GraphicsLayer();
// 使用add(graphic)方法
graphicslayer.add(new Graphic(geo, fill));
// 使用Map的addLayer(layer)方法添加图层
map.addLayer(graphicslayer);
// 对graphicslayer绑定click事件
graphicslayer.on("click", function(evt) {
console.log(evt);
}
)}