deactivate.doBuffer

941
5
02-17-2011 12:08 AM
simonmiles
New Contributor II
Hi,

I'm going back to basics and working with a sample. What i'm trying to do and failing is in the first instance is to deactivate the doBuffer function and only activate it when i clicking on a button.

I'm using this esri sample
http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm#jssa...

what i've tried and failed with is removing "dojo.connect(map, "onClick", doBuffer);" from the function initialize...

gsvc = new esri.tasks.GeometryService("http://sampleserver1.arcgisonline.com/arcgis/rest/services/Geometry/GeometryServer");
      dojo.connect(map, "onClick", doBuffer);

adding in doBuffer.deactivate (); in the below funcation....

function doBuffer(evt) {
      doBuffer.deactivate ();
      map.graphics.clear();
      var symbol = new esri.symbol.SimpleMarkerSymbol();
      var graphic = new esri.Graphic(evt.mapPoint, symbol);

then in the body adding in a button....

<button type="button" onClick="doBuffer.activate();">Click Me!</button>

But its just not happening, where am i going wrong?

Any help would be great

Regards

Simon
0 Kudos
5 Replies
MarioObendorfer
New Contributor
Hi,

I'm a little bit confused about your code but I'll try to explain what you should do when you try to de/activate some functionality with buttonclick ...

Connect your button button with onclick handler for the map ... e.g.:

<button onclick="javascript: eventhandle = dojo.connect(map, ev, fun)">

fun = function(event) {
//place business logic here => e.g. doBuffercode
dojo.disconnect(eventhandle); //disconnect this evenhandler => only one buffering is done ... you can place this line of code on antoher button or wherever you want to trigger the deactivation
}

The code you wrote doBuffer.deactivate() should do nothing because doBuffer is a function itselfe and not a object you can call a function on. In the esri example the activate() and deactivate() functions are called on the drawtoolbar object. I think you mixed those things up.

Hope that helps .. if not post more of your code.

Greets Tol
0 Kudos
HemingZhu
Occasional Contributor III
Hi,

I'm going back to basics and working with a sample. What i'm trying to do and failing is in the first instance is to deactivate the doBuffer function and only activate it when i clicking on a button.

I'm using this esri sample
http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples_start.htm#jssa...

what i've tried and failed with is removing "dojo.connect(map, "onClick", doBuffer);" from the function initialize...

gsvc = new esri.tasks.GeometryService("http://sampleserver1.arcgisonline.com/arcgis/rest/services/Geometry/GeometryServer");
      dojo.connect(map, "onClick", doBuffer);

adding in doBuffer.deactivate (); in the below funcation....

function doBuffer(evt) {
      doBuffer.deactivate ();
      map.graphics.clear();
      var symbol = new esri.symbol.SimpleMarkerSymbol();
      var graphic = new esri.Graphic(evt.mapPoint, symbol);

then in the body adding in a button....

<button type="button" onClick="doBuffer.activate();">Click Me!</button>

But its just not happening, where am i going wrong?

Any help would be great

Regards

Simon


Try this: put onclickConnect = dojo.connect(map, "onClick", doBuffer); in your button's onClick handler to activate the map click event. Then put this: dojo.disconnect(onclickConnect); in your doBuffer method to deactivate or dissconnect map click event. Note: set onclickConnect as a global variable.
0 Kudos
simonmiles
New Contributor II
hi hzhu,

thanks for your help and i've added in yoor code but there is still something up with my code.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>Buffer</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css">
<script src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6" type="text/javascript"></script>

<script type="text/javascript">
dojo.require("esri.map");
dojo.require("esri.tasks.geometry");

var map = null;
var geometryService = null;
var globals;


function initialize() {
map = new esri.Map("map");
var layer = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
map.addLayer(layer);
map.setExtent(new esri.geometry.Extent(-97.76, 32.32, -96.11, 33.42, new esri.SpatialReference({wkid: 4326})));

geometryService = new esri.tasks.GeometryService("http://sampleserver1.arcgisonline.com/arcgis/rest/services/Geometry/GeometryServer");

}

function doBuffer(evt) {
var globals = dojo.disconnect(map, "onClick", doBuffer);
map.graphics.clear();
var symbol = new esri.symbol.SimpleMarkerSymbol();
var graphic = new esri.Graphic(evt.mapPoint, symbol);

var params = new esri.tasks.BufferParameters();
params.features = [ graphic ];

// CASE [1]: if you want to buffer in angular units.
//params.distances = [ 5, 10 ];
//params.unit = esri.tasks.BufferParameters.UNIT_DEGREE;

// CASE [2]: if you want to buffer in linear units such as meters, km, miles etc.
params.distances = [ 2500 ];
params.unit = esri.tasks.BufferParameters.METERS;
params.bufferSpatialReference = new esri.SpatialReference({wkid: 32662});
params.outSpatialReference = map.spatialReference;

geometryService.buffer(params, showBuffer);
}

function showBuffer(features) {

var symbol = new esri.symbol.SimpleFillSymbol(
esri.symbol.SimpleFillSymbol.STYLE_SOLID,
new esri.symbol.SimpleLineSymbol(
esri.symbol.SimpleLineSymbol.STYLE_SOLID,
new dojo.Color([0,0,255,0.65]), 2
),
new dojo.Color([0,0,255,0.35])
);

for (var j=0, jl=features.length; j<jl; j++) {
features.setSymbol(symbol);
map.graphics.add(features);
}
}

dojo.addOnLoad(initialize);
</script>

</head>

<body class="tundra">
<b>Click a location on the map to buffer. Click again on another location to buffer again.</b>
<br/>

<button type="button" onClickConnect = dojo.connect(map, "onClick", doBuffer);>Click Me!</button>
<div id="map" style="width:600px; height:400px; border:1px solid #000;"></div>
</body>

</html>
0 Kudos
MarioObendorfer
New Contributor
Hi,



    function doBuffer(evt) {
      var globals = dojo.disconnect(map, "onClick", doBuffer);
      ...
    }

    <button type="button" onClickConnect = dojo.connect(map, "onClick", doBuffer);>Click
[/SIZE]


Change this 2 lines in your code to ....

<input type="button" onclick="globals = dojo.connect(map, 'onClick', doBuffer); " value="Click Me!">


and

function doBuffer(evt) {
      dojo.disconnect(globals);
      ...
    }


A little bit explained .... you need the global var globals for saving a handle to your eventhandler. Assign (=> globals = dojo.connect(etc)) the eventhandler when you click on the button and disconnect the handle when the doBuffer method is calls (=> dojo.disconnect(globals)).

Hope this will help this time ....

Further you should really start reading some JavaScript related artikels about scope and about dojo eventhandling or simply the dojo API.

Greets Tol
0 Kudos
simonmiles
New Contributor II
Tol,

Thank you very much for this.

Si
0 Kudos