Add SimpleMarkerSymbol to Geocoder

3198
2
Jump to solution
06-16-2015 12:13 PM
GiovanniMarrero3
New Contributor III

I'm currently updating the Geocoder in WAB to work like the link below but I can't figure out how to add a marker when an address is found. Can someone tell me why this code is not adding a graphic after inputting an address in the geocoder?

Trigger Layer(s) Popup with Feature.Geometry

      onFindResults: function(results) {

        //Check to see if the user typed an MGRS or USNG string

        this.lastMgrsResult = this.lookupMgrs(results.results.value);

        if (null !== this.lastMgrsResult) {

          // Get reference ellipsoid coordinates of the current coordinate system

          // var mapWkid = this.map.spatialReference.wkid;

          // var srWkid = csUtils.isProjectedCS(mapWkid) ?

          //csUtils.getGeoCSByProj(mapWkid) : mapWkid;

          //Make a new object just like the objects in results.results.results

          var point = new Point(

            this.lastMgrsResult.longitude,

            this.lastMgrsResult.latitude,

            new SpatialReference({

              wkid: 4326 //srWkid   should use the ellipsoid SR of the current coordinate system

            }));

          var extent = this.map.extent.centerAt(point);

   var symbol = new SimpleMarkerSymbol();

  symbol.setStyle(SimpleMarkerSymbol.STYLE_SQUARE);

  symbol.setColor(new Color([153,0,51,0.75]));

          var graphic = new esri.Graphic(point, symbol);

   map.graphics.add(graphic);

          var resultObject = {

            extent: extent,

            feature: feature,

            name: this.lastMgrsResult.text

          };

          //Insert at start of results list

          results.results.results.unshift(resultObject);

        }

      },

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Giovanni,

  1. Did you add the requires for Point, SpatialReference, SimpleMarkerSymbol, Color, Graphic ?
  2. In a WAB widget you have to use this.map and you only have map.graphics.add(graphic);
  3. You have this "feature: feature," in your code and I don't see where feature is defined anywhere.

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

Giovanni,

  1. Did you add the requires for Point, SpatialReference, SimpleMarkerSymbol, Color, Graphic ?
  2. In a WAB widget you have to use this.map and you only have map.graphics.add(graphic);
  3. You have this "feature: feature," in your code and I don't see where feature is defined anywhere.
GiovanniMarrero3
New Contributor III

Thank you for taking the time to answer this question. I had the three problems you mentioned, I was missing the "esri/Color" in the define section and I needed the "this.map." to map.graphics.add(graphic), and I had it in the wrong section of the script. Here's the working code.

if (feature) {

      var mpPt = feature.geometry;

  this.map.centerAndZoom( mpPt, 20);
  var symbol = new SimpleMarkerSymbol();
     symbol.setStyle(SimpleMarkerSymbol.STYLE_SQUARE);
     symbol.setColor(new Color([255,0,0]));

      var graphic = new esri.Graphic(mpPt, symbol);

  this.map.graphics.add(graphic);

      setTimeout(lang.hitch(this, function(){ var scrPt = this.map.toScreen(mpPt); this.map.emit("click", { bubbles:           true, cancelable: true, screenPoint: scrPt, mapPoint: mpPt }); }), 1000);

      } else if (extent) {

         this.map.setExtent(extent);

      }

Thanks again!

0 Kudos