Hello!
I am trying to create a basic buffer widget using GeometryService. Whenever I click on my map, the firebug console throws this error:
GET http://tasks.arcgisonline.com/ArcGIS/rest/info?f=json
TypeError: b.getExtent is not a function
Any ideas would be greatly appreciated. Thank you!
TA
Solved! Go to Solution.
TA,
The CORs check error is one that is standard and you will not get rid of this. You can safely ignore that error.
Ta,
The issue you are having is you are trying to create a graphic from the symbology only and you are not passing it the buffers geometry.
Here is what you should be using:
var graphic = new Graphic(evt[0], symbol);
But still that would not get you both rings that you buffered so this is better code:
define(['dojo/_base/declare', 'jimu/BaseWidget', 'esri/map', 'esri/tasks/BufferParameters', 'esri/tasks/GeometryService', 'esri/graphic', 'esri/symbols/SimpleFillSymbol', 'esri/symbols/SimpleLineSymbol', 'esri/Color', 'dojo/_base/array' ], function (declare, BaseWidget, Map, BufferParameters, GeometryService, Graphic, SimpleFillSymbol, SimpleLineSymbol, Color, array) { //To create a widget, you need to derive from BaseWidget. return declare([BaseWidget], { // Custom widget code goes here name: 'BufferTool', baseClass: 'jimu-widget-buffertool', postCreate: function () { this.inherited(arguments); var map = this.map; var gsvc = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); map.on('click', function (evt) { var params = new BufferParameters(); params.geometries = [evt.mapPoint]; //buffer in linear units such as meters, km, miles etc. params.distances = [1, 2]; params.unit = GeometryService.UNIT_STATUTE_MILE; params.outSpatialReference = map.spatialReference; gsvc.buffer(params, function (geoms) { geoms.reverse(); var symbol = new SimpleFillSymbol( SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 0, 255, 0.65]), 2), new Color([0, 0, 255, 0.35]) ); var symbol2 = new SimpleFillSymbol( SimpleFillSymbol.STYLE_SOLID, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0, 0.65]), 2), new Color([255, 0, 0, 0.35]) ); var syms = [symbol, symbol2]; var graphic; array.map(geoms, function(bufferGeom, index){ graphic = new Graphic(bufferGeom, syms[index]); map.graphics.add(graphic); }); }); }); }, //--------------------------------------- startup: function () { this.inherited(arguments); console.log('startup'); } }); });
Hi Robert,
Thank you for your response! Hmmm, when I use your code, it's still throwing the same error. Womp.
As I'm reading it over, I'd like to ask a few questions.
1. just to be sure, "geoms" an array of the polygon geometries? why would you want to reverse the array?
2. how come it's ok to use array.map() when you haven't defined an Array object called "array"?
3. why not use geoms.forEach()?
4. why use new Graphic(bufferGeom, syms[index]) instead of new Graphic(geoms, syms[index]? (how would "index" know it's values are [0, 1]?
Does this code work for you?
Thanks Robert!
TA
Ta,
Yes this is code I tested.
just to be sure, "geoms" an array of the polygon geometries? why would you want to reverse the array?
Yes geoms is just the returned array of polygon geometries. I reverse the array so that the one mile buffer is added on top of the 2 mile buffer.
how come it's ok to use array.map() when you haven't defined an Array object called "array"?
I have defines array as the very last require and the last var.
why not use geoms.forEach()
Using dojo base array is a proven cross browser utility. I use dojo libraries when ever they are available since JS uses dojo anyway.
why use new Graphic(bufferGeom, syms[index]) instead of new Graphic(geoms, syms[index]
geoms is the array of geoms and when creating a graphic it is expecting a single graphic.
how would "index" know it's values are [0, 1]
The utility of use dojos array is that when using it it keeps up with the index for you each time the array loops the index changes. I know that there are only two geoms returned from the buffer task so the index is only going to be 0 and 1.
Hi Robert,
Thank you for your answers to the coding questions- definitely helped. As for the "Cross Origin Request Block" message, do you know how to address this? (perhaps an issue with my server?). When I click on my map, nothing happens (no buffers, no messages on the console).
Thanks!
TA
TA,
The CORs check error is one that is standard and you will not get rid of this. You can safely ignore that error.
Hi Robert!
If the CORS error isn't a problem, does the problem lie with the code?
Sorry if that's a dumb question.
TA
Hi Robert,
I finally got my widget to work! The code you provided helped as well as creating a new web app (using "Create New" on the web app builder page. I don't know why creating a new app solved the issue, but that's what an Esri customer service analyst told me). Thank you! (:
TA