Select to view content in your preferred language

add graphic (point) from url parameters, point doesn't display

527
1
06-25-2014 08:13 AM
MikeSteven
Regular Contributor
Hi,

I'm trying to have a simple map that will show an ArcGISTiledMapServiceLayer as the basemap. On top of this I want to add a graphic (a point) created with x and y values given as url parameters. The first problem I had was that I was getting an error saying map.graphics is null. I came across this forum post that says that I need to wait until the map load event is fired before adding the graphic. I tested that this works by manually typing x and y coordinates into the arguments I'm passing into the mapLoadHandler method like so:

map.on("load", function(){mapLoadHandler(325000, 675000);});

But in reality I want to pass the x and y values from the url parameters as arguments into the mapLoadHandler method and if I do this then the graphic doesn't appear (and there aren't any error messages). Interestingly the map does centerAndZoom successfully to the location of the x and y coordinates passed into the method but I don't understand why it then doesn't draw the graphic. Here is the code:

     var xcoord = new Number(getUrlVars()["x"]);
     var ycoord = new Number(getUrlVars()["y"]);     
     map.on("load", function(){mapLoadHandler(xcoord, ycoord);});

     function getUrlVars() {
      var vars = {};
      var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
       vars[key] = value;
      });
      return vars;
     }
     
     function mapLoadHandler(xcoord, ycoord) {
      //resizeMap();      
      var incident = new Point([xcoord, ycoord], new SpatialReference({
        wkid : 27700
      }));
      console.log("incident.x = " + incident.x);
      map.centerAndZoom(incident, 7);
      var symbol = SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 18, new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([0, 105, 255]), 2), new Color([255, 0, 0]));      
      var newGraphic = new Graphic(incident, symbol);
      map.graphics.add(newGraphic);
     }


Cheers,
Mike
0 Kudos
1 Reply
MikeSteven
Regular Contributor
I've worked it out.

var xcoord = new Number(getUrlVars()["x"]);
var ycoord = new Number(getUrlVars()["y"]);

creates objects

var xcoord = Number(getUrlVars()["x"]);
var ycoord = Number(getUrlVars()["y"]);

creates numbers. And now that the url parameters are numbers the point is displaying
0 Kudos