Draw a polygon given an ordered list of lat/lon points?

1131
14
Jump to solution
03-19-2014 11:54 AM
DanielMauer
New Contributor II
I have a map, the projection is Web Mercator.  My web application is going to be calling (via javascript external interface) a function which will receive an object as input like this:
// JavaScript  var inputObject = {       polygon: [ [40.6701, 73.94001], [40.6724, 73.94012], [40.6902, 74.02011] ],      title: "My Selected Region" }  document.getElementById("myMap").SetSelectedPolygonFunction(inputObject); 


...I've already got a graphics layer on the map ("myGraphicsLayer").

Now, what I need to do is write this function:

// Flex map code  // In initialization function: // ....    ExternalInterface.addCallback("SetSelectedPolygon", setSelectedPolygon); // ....etc   private function setSelectedPolygon(data:Object = null):void {    var thePolygon:Polygon = doSomethingOrOtherTo(data.polygon); // What do I do to data.polygon to make this function work?   var myGraphic:Graphic = new Graphic();  myGraphic.geometry = thePolygon;   myGraphicsLayer.add(myGraphic);  }
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor
Daniel,

   That sounds likely. I think you are going to have to parse the JSON further as the more I think about it the addRing is expecting an array of MapPoints not an array of numbers.

once again this is me free handing this code so there maybe mistakes
var mpArr:Array = []; var pArr:Array = JSONUtil.decode(data.polygon); for (var p:int=0; p<pArr.length; p++){     var mp:MapPoint(pArr

[0],pArr

[1], new SpatialReference(4326));     mpArr.push(mp); } thePolygon.addRing(mpArr);

View solution in original post

0 Kudos
14 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor
Daniel,

   Just writing code of the top of my head, I would try something like this (of course this is missing the imports):

var thePolygon:Polygon = new Polygon(null, new SpatialReference(4326));
thePolygon.addRing(JSONUtil.decode(data.polygon));
var wmPoly:Polygon = WebMercatorUtil.geographicToWebMercator(thePolygon);
0 Kudos
DanielMauer
New Contributor II
That seems simple enough -- what exactly is the new SpatialReference(4326) about? Forgive the newbieness.

Daniel,

   Just writing code of the top of my head, I would try something like this (of course this is missing the imports):

var thePolygon:Polygon = new Polygon(null, new SpatialReference(4326));
thePolygon.addRing(JSONUtil.decode(data.polygon));
var wmPoly:Polygon = WebMercatorUtil.geographicToWebMercator(thePolygon);
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor
Daniel,

  Because those coordinates that you are getting are not web mercator, they look like WGS1984 lat Lon coordinates.
0 Kudos
DanielMauer
New Contributor II
I figured it was something along those lines.  So basically I'm defining the polygon in WGS1984 lat/lon, then using the web mercator utilities to convert it into web mercator coordinates, and then putting it on the graphics layer.

Thanks!

Daniel,

  Because those coordinates that you are getting are not web mercator, they look like WGS1984 lat Lon coordinates.
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor
Daniel,

   Yep, that is right.


Don't forget to click the Mark as answer check on this post and to click the top arrow (promote).
Follow these steps as shown in the below graphic:

0 Kudos
DanielMauer
New Contributor II
Getting close.  After the addRing call, when I spit out the contents of thePolygon.toString(), I get this:


(com.esri.ags.geometry::Polygon)#0
  hasM = false
  hasZ = false
  rings = (Array)#1
    [0] (Array)#2
      [0] (Array)#3
        [0] 40.6701
        [1] 73.94001
      [1] (Array)#4
        [0] 40.6724
        [1] 73.94012
      [2] (Array)#5
        [0] 40.6902
        [1] 74.02011
  spatialReference = (com.esri.ags::SpatialReference)#6
    vcsWkid = NaN
    wkid = 4326
    wkt = (null)
  type = "esriGeometryPolygon"


... The next line, though (var wmPoly:Polygon = WebMercatorUtil.geographicToWebMercator(thePolygon);) , is breaking.  Since geographicToWebMercator returns a Geometry, I need to add an "as Polygon" on the end of it so it knows specifically that this is a Polygon geometry.  So I put that in place.  But, I'm getting an error on that line, and being as I'm calling this from JavaScript, the error message is unhelpful:

[INDENT]"Error: Error calling method on NPObject.
    at <anonymous>:2:17
    at Object.InjectedScript._evaluateOn (<anonymous>:613:39)
    at Object.InjectedScript._evaluateAndWrap (<anonymous>:573:52)
    at Object.InjectedScript.evaluate (<anonymous>:492:21)"[/INDENT]

..Any idea what the problem might be?

Daniel,

   Just writing code of the top of my head, I would try something like this (of course this is missing the imports):

var thePolygon:Polygon = new Polygon(null, new SpatialReference(4326));
thePolygon.addRing(JSONUtil.decode(data.polygon));
var wmPoly:Polygon = WebMercatorUtil.geographicToWebMercator(thePolygon);
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor
Daniel,

  Well, not much to go one there... So you do have the import for WebMercatorUtil right?
0 Kudos
DanielMauer
New Contributor II
I definitely have the import.

So... I stuck it in a try/catch block to figure out what the real error is.  Type coercion failed.  So, even though thePolygon is in fact a Polygon, the following is throwing a Type Coercion error:

    
var wmPoly:Polygon = WebMercatorUtil.geographicToWebMercator(thePolygon) as Polygon;


...Is this not the right way to convert the polygon to web mercator?

Daniel,

  Well, not much to go one there... So you do have the import for WebMercatorUtil right?
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor
Daniel,

   Sure that is right. For debugging try this to see if you get past this line of code:

var geom:Geometry = WebMercatorUtil.geographicToWebMercator(thePolygon);
0 Kudos