Extent from array at points?

2310
4
Jump to solution
05-23-2013 09:02 AM
EduardoCarvalho
New Contributor
Hi,

i'm starting developer with API for JavaScript and i can't get Extent from array at points. I looked the documentation and the atritute graphics from MAP class is GraphicLayer, but the GraphicLayer don't have method that return Extent class.

How can I get Extent from array at points?

var point = new esri.geometry.Point(-46.646297, -23.545901); var point2 = new esri.geometry.Point(-46.639988, -23.548556); var defaultSymbol = new esri.symbol.PictureMarkerSymbol('http://static.arcgis.com/images/Symbols/Shapes/GreenPin1LargeB.png', 55, 45);       var graphic = new esri.Graphic(point, defaultSymbol);  var graphic2 = new esri.Graphic(point2, defaultSymbol);  map.graphics.add(graphic);    map.graphics.add(graphic2); 


thanks for any help

obs. sorry for my english =\
0 Kudos
1 Solution

Accepted Solutions
SteveCole
Frequent Contributor
Ok, two things-

For some reason, the map graphics layer has a feature with a 0,0 coordinate extent so that's why the zeros show up in your popup. I inserted this line of code into yours just before you add your points to the map graphics and the zero dimension graphic was removed:

   map.graphics.clear();    map.graphics.add(graphic);       map.graphics.add(graphic2);


I only found this by using a DOM element inspection tool and looked at the map.graphics.graphics object and saw that it had three items in it instead of the two that you would expect.

The second thing, and I don't know if this is really critical or not, was that your coordinate pairs were missing the spatial projection so I added it like this:

   var point = new esri.geometry.Point(-73.986268, 40.735812,new esri.SpatialReference({ wkid: 4326 }));    var point2 = new esri.geometry.Point(-77.03756, 38.907332,new esri.SpatialReference({ wkid: 4326 }));

View solution in original post

0 Kudos
4 Replies
SteveCole
Frequent Contributor
Use esri.graphicsExtent and pass it the array of graphics.
0 Kudos
EduardoCarvalho
New Contributor
evtguy thanks for replay.

But the Extent class return two attributes with ZERO: xmax and ymin, then when I put the Extent class in map (map.setExtent), the map goes to ocean.

Look the sample

<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=7, IE=9, IE=10">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">

  <link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/esri/css/esri.css">
  <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/js/dojo/dijit/themes/claro/claro.css">

  <script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.5/"></script>   
  
  <style>
    html, body, #mapDiv {
   height:100%;
   width:100%;
   margin:0;
   padding:0;
    }
  </style>  
  
  <script type="text/javascript">
    var djConfig = { parseOnLoad: true };
  </script>  
  
  <script>
  dojo.require("esri.map");
     
  var map;
       
  function init() { 
   
   map = new esri.Map("mapDiv", {     
     basemap: "streets",
     sliderStyle: "large"
   });
   
   dojo.connect(map, "onLoad", load);
  }
  
  function load(){  
   var point = new esri.geometry.Point(-73.986268, 40.735812);
   var point2 = new esri.geometry.Point(-77.03756, 38.907332);
   var defaultSymbol = new esri.symbol.PictureMarkerSymbol('http://static.arcgis.com/images/Symbols/Shapes/GreenPin1LargeB.png', 55, 45);     

   var graphic = new esri.Graphic(point, defaultSymbol); 
   var graphic2 = new esri.Graphic(point2, defaultSymbol);

   map.graphics.add(graphic);   
   map.graphics.add(graphic2);
   
   var extent = esri.graphicsExtent(map.graphics.graphics);
   
   /*
   return Extent:
    xmax: 0
    xmin: -77.03756
    ymax: 40.735812
    ymin: 0
   */
   
   var coordenatesExtent = "Return Extent from esri.graphicsExtent(map.graphics.graphics) \n\n" +
         "xmax: " + extent.xmax + " \n" +
         "xmin: " + extent.xmin + " \n" +
         "ymax: " + extent.ymax + " \n" +
         "ymin: " + extent.ymin;
         
   alert(coordenatesExtent);
   
   map.setExtent(extent);
  } 

  dojo.ready(init);
  
  </script>
  <title></title>
 </head>
  
 <body class="claro">  
  <div id="mapDiv">
  </div>
 </body>
  
</html>
0 Kudos
SteveCole
Frequent Contributor
Ok, two things-

For some reason, the map graphics layer has a feature with a 0,0 coordinate extent so that's why the zeros show up in your popup. I inserted this line of code into yours just before you add your points to the map graphics and the zero dimension graphic was removed:

   map.graphics.clear();    map.graphics.add(graphic);       map.graphics.add(graphic2);


I only found this by using a DOM element inspection tool and looked at the map.graphics.graphics object and saw that it had three items in it instead of the two that you would expect.

The second thing, and I don't know if this is really critical or not, was that your coordinate pairs were missing the spatial projection so I added it like this:

   var point = new esri.geometry.Point(-73.986268, 40.735812,new esri.SpatialReference({ wkid: 4326 }));    var point2 = new esri.geometry.Point(-77.03756, 38.907332,new esri.SpatialReference({ wkid: 4326 }));
0 Kudos
EduardoCarvalho
New Contributor
Now everthing make a sence.

I had not seen that the array contained 3 feature. The first feature with 0,0 coordinate has SpatialReference wkid: 102100 and my two points has SpatialReference wkid: 4326 (obs: by default the Point class create the spatialreferente 4326, you don't need to inform).

When I get Extent class from esri.graphicsExtent, the method graphicsExtent get the first object from array and use the same spatialreference for the Extent class return. Before, I didn't understand why the spatialreferente of Extent return was 102100 and not 4326.

Thus, when I put the Extent return to map.setExtent the map go to ocean.

evtguy thank you very much for your patience and help. Now I can go to sleep peacefully 😃

I was searching all day long about it.
0 Kudos