在web操作,如绘制或丈量的时候,为了精确,需要捕捉到某1图层的对象,在此,讲授下如何在Arcgis for JS中实现捕捉对象。
首先,在地图中添加1个graphiclayer:
gLayer = new GraphicsLayer();
map.addLayer(gLayer);
接着,在gLayer中添加几个对象:
map.on("load",function(){
var items = geometry.item;
gLayer.add(new Graphic(WktToPoint(items[0].wkt, {"wkid":4326}),sms));
gLayer.add(new Graphic(WktToPolyline(items[1].wkt, {"wkid":4326}),sls));
gLayer.add(new Graphic(WktToPolygon(items[2].wkt, {"wkid":4326}),sfs));
});
接下来,定义drawtoolbar和绘制完成后的事件:
var drawToolbar = new esri.toolbars.Draw(map,{tooltip: "place text"});
drawToolbar.on("draw-end",showDrawResults);
function showDrawResults(evt){
drawToolbar.deactivate();
map.setMapCursor("default");
var geometry = evt.geometry;
map.graphics.add(new Graphic(geometry,sls));
};
接下来,定义1个Button,并添加button的click事件:
<button id="draw" style="z-index: 99;">绘制图形</button>
on(dom.byId("draw"), "click", function(){
drawToolbar.activate(esri.toolbars.Draw.POLYLINE);
});
接下来定义map的捕捉设置:
var snapManager = map.enableSnapping({
snapKey:has("mac") ? keys.META : keys.CTRL
});
var layerInfos = [{layer: gLayer}];
snapManager.setLayerInfos(layerInfos);
备注:
此处,需要调用的dojo包包括:
require([
"esri/map",
"esri/layers/GraphicsLayer",
"esri/graphic",
"esri/SnappingManager",
"esri/sniff",
"dojo/_base/event",
"dojo/on",
"dojo/dom",
"dojo/keys",
"dojo/domReady!"],
function(Map, GraphicsLayer, Graphic, Point, SnappingManager, has,
event, on,dom, keys) {
如有疑问请联系:
QQ:1004740957
Email:niujp08@qq.com
请关注微信公众账号lzugis15,随时了解更多GIS动态信息。