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

1982
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
14 Replies
DanielMauer
New Contributor II
I get the same error.  Is it possible that despite happily printing out its geometry (per above post), thePolygon somehow contains some screwy data that's causing geographicToWebMercator to croak?

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
RobertScheitlin__GISP
MVP Emeritus
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);
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Daniel,

   The other thing that occurs to me is where are those coordinates suppose to be in the world? For both the lat and Lon to be positive numbers you would be talking about some where in Asia if you are passing Lat then Lon?..
0 Kudos
DanielMauer
New Contributor II
That was it!  I think there's something up with the values I'm putting in in the first place, but from this point I'm sure I can handle it.  Converting each point into a MapPoint makes the conversion system happy.

Thanks!

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);
0 Kudos
DanielMauer
New Contributor II
Oh, those were just random-ish numbers. The actual numbers will be coming out of a database.

Daniel,

   The other thing that occurs to me is where are those coordinates shose to be in the world? For both the lat and Lon to be positive numbers you would be talking about some where in Asia if you are paasing Lat then Lon?..
0 Kudos