Select to view content in your preferred language

Buffer runs multiple times

1157
4
Jump to solution
10-09-2014 12:06 PM
TimWitt2
MVP Alum

Hey everybody,

In my drawing app, I want  to add the possibility of buffering a point. I got it to work, but I have one issue.

When I run it the first time everything goes ok. User checks the buffer checkbox, types in a buffer distance, selects the point button, clicks on the map and the buffer gets added to the map.

When I run it a 2nd time, it looks like it works again like it is supposed to, but this time two identical buffer get added. When I do it a third time 3 identical buffers get added and so on.

It seems that geometryService.on("buffer-complete") runs as often as I used the buffer before.

Here is the code snipped that is responsible for adding the buffer:

  function addToMap(evt) {

       Uni();

       if ( evt === undefined){

            console.log("undefined");

       } else {

            toolbar.deactivate();

            var Testcolor = cp.value;

       if (Testcolor == ""){

            newColor = newColor;

       } else {

            docolor();

       }

       switch (evt.geometry.type) {

            case "point":

            case "multipoint":

            symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, myNumber,

            new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,

            new Color(newColor), 1),

            new Color(newColor));

            break;

            case "polyline":

            symbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color(newColor), myNumber)

            break;

            default:

            symbol = new SimpleFillSymbol(symbolfill, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,

            new Color(newColor), myNumber), new Color(newColor));

            break;

       }

       graphic = new Graphic(evt.geometry, symbol);

       if (dijit.byId("BufferCheckBox").checked && evt.geometry.type == "point" ){

            params = new BufferParameters();

            params.geometries  = [ evt.geometry ];

            params.distances = [ dom.byId('BufferText').value ];

            params.geodesic = true;

            params.bufferSpatialReference = new SpatialReference({wkid: 102100});

            params.outSpatialReference = map.spatialReference;

            params.unit = Unit_Line;

            geometryService.buffer(params);

            geometryService.on("buffer-complete", function(evt) {

                 var Buffsymbol = new SimpleFillSymbol("none", new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color(newColor), 2), new Color(newColor));

                 Buffgraphic = new Graphic(evt.geometries[0], Buffsymbol);

                 map.graphics.add(Buffgraphic);

                 var operation2 = new CustomOperation.Add({

                      graphicsLayer: map.graphics,

                      addedGraphic: Buffgraphic

            });

            undoManager.add(operation2);

       });

      }

Any help is appreciated!

Thanks

Tim

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

The way it's currently written, every time you run the function addToMap, you add another listener for the geometryService's buffer-complete event. You have to either move that code outside your function or remove the listener once it's finished.

View solution in original post

0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor

The way it's currently written, every time you run the function addToMap, you add another listener for the geometryService's buffer-complete event. You have to either move that code outside your function or remove the listener once it's finished.

0 Kudos
TimWitt2
MVP Alum

Thanks Ken,

can't believe I missed that, especially since I already have listeners in my app!

Tim

0 Kudos
StevenGraf1
Frequent Contributor

Maybe the buffer is thinking that it is suppose to be using a multipoint?

  1. case "point"
  2.             case "multipoint"
  3.             symbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, myNumber, 
  4.             new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, 
  5.             new Color(newColor), 1), 
  6.             new Color(newColor)); 
  7.             break
0 Kudos
TimWitt2
MVP Alum

Thanks Steven!

Ken's suggestions answered my question.

0 Kudos