on.once(map, 'click', function(evt) { //do stuff });
var clickTheMap = on(map, 'click', function(evt) { //remove event when fired (equivalent to on.once) clickTheMap.remove(); clickTheMap = null; //do stuff }); on(myCancelButton, 'click', function() { if(clickTheMap) { clickTheMap.remove(); } });
/* map event handling */ // primarily used for map 'click' events // however any on-style event can be added // does not support connect events eventAdd: function (event) { this.eventOns.push(event); }, eventBegin: function () { this.infoWindow.hide(); this.infoWindow.clearFeatures(); this.setMapCursor('default'); this.eventMouseEventsDisable(); arrayUtils.forEach(this.eventOns, function (o) { o.remove(); }); this.eventOns = []; if (this.drawToolbar._geometryType != null) { this.drawToolbar.deactivate(); } var s = esriBundle.toolbars.draw; s.addPoint = 'Click to add a point'; s.freehand = 'Press down to start and let go to finish'; s.start = 'Click to start drawing'; s.resume = 'Click to continue drawing'; s.complete = 'Double-click to finish'; }, eventEnd: function () { this.setMapCursor('default'); this.eventMouseEventsEnable(); this.toaster.hide(); }, eventReset: function () { this.eventBegin(); this.eventEnd(); }, eventMouseEventsEnable: function () { arrayUtils.forEach(this.graphicsLayerIds, function (layer) { this.getLayer(layer).enableMouseEvents(); }, this); }, eventMouseEventsDisable: function () { arrayUtils.forEach(this.graphicsLayerIds, function (layer) { this.getLayer(layer).disableMouseEvents(); }, this); },
this.map.eventBegin(); this.map.setMapCursor('crosshair'); this.map.drawToolbar.activate('point'); var o = on.once(this.map.drawToolbar, 'draw-end', lang.hitch(this, function (result) { this.map.eventEnd(); //do stuff })); this.map.eventAdd(o);
//Identify task function initFunctionality (map){ dojo.connect(map, "onClick", executeIdentifyTask); identifyTask = new esri.tasks.IdentifyTask("http://gismap.oit.ncsu.edu/arcgis/rest/services/FiberAnalytics_IMS/MapServer"); identifyParams = new esri.tasks.IdentifyParameters(); identifyParams.tolerance = 2; identifyParams.returnGeometry = true; identifyParams.layerIds = [2,3,4,5,6,7,8,9,11,18]; identifyParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_TOP; identifyParams.width = map.width; identifyParams.height = map.height; } //Execute Identify Task function executeIdentifyTask(evt){ identifyParams.geometry = evt.mapPoint; identifyParams.mapExtent = map.extent; var deferred = identifyTask.execute(identifyParams); deferred.addCallback(function(response){ // response is an array of identify result objects // Let's return an array of features. return dojo.map(response, function(result){ var feature = result.feature; feature.attributes.layerName = result.layerName;
var connects = []; function reset() { dojo.forEach(connects, dojo.disconnect); connects = []; }; //elsewhere reset(); var mapClick = dojo.connect(map, 'onClick', function(evt) { dojo.disconnect(mapClick); //do stuff }); connects.push(mapClick);
require([ 'dojo/on', 'dojo/query', 'esri/map', 'esri/tasks/IdentifyTask', 'esri/tasks/IdentifyParameters', 'esri/layers/ArcGISDynamicMapServiceLayer', 'dojo/domReady!' ], function ( on, query, Map, IdentifyTask, IdentifyParameters, ArcGISDynamicMapServiceLayer ) { var map, layer, idTask, idParams, handler, paused = false, $button = query('#btnIdentify')[0]; map = new Map('mapDiv', { basemap: 'streets', center: [-83.275, 42.573], zoom: 18 }); layer = new ArcGISDynamicMapServiceLayer('http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer', { opacity: 0.5 }); map.addLayer(layer); on.once(map, 'load', function () { // define a pausable handler handler = on.pausable(map, 'click', doIdentify); idTask = new IdentifyTask('http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/Parcels/MapServer'); idParams = new IdentifyParameters(); idParams.tolerance = 3; idParams.returnGeometry = true; idParams.layerIds = [0, 2]; idParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL; idParams.width = map.width; idParams.height = map.height; }); // handle the pausable handler on($button, 'click', function () { paused = !paused; if (paused) { handler.pause(); $button.innerHTML = 'Paused'; } else { handler.resume(); $button.innerHTML = 'Not Paused'; } }); function doIdentify(e) { console.debug('identify on the map'); idParams.geometry = e.mapPoint; idParams.mapExtent = map.extent; idTask.execute(idParams).then(function () { alert('Identify Complete'); }, function (err) { alert('Error, sorry'); }); } });
var mapClick, allowIdentify = true; function initConnect() { mapClick = dojo.connect(map, 'onClick', function(evt) { dojo.disconnect(mapClick); //do stuff }); } function disconnect() { dojo.disconnect(mapClick); } // some sort of action, like a button click dojo.connect(dojo.byId('btnIdentify'), 'onClick', function() { allowIdentify = !allowIdentify; if (allowIdentify) { disconnect(); } else { initConnect(); } });