How do you get the extent and center of your current view?

2383
4
06-06-2013 12:34 PM
SethCrotchett
New Contributor
I am trying to get the center point (lat, and long) of my current view.

I tried mymap.extent.getCenter(); but that doesnt seem to work.  Any ideas?
0 Kudos
4 Replies
KellyHutchins
Esri Frequent Contributor
Getting the map extent's center point should work. Here's a code snippet showing this:

    <script>
      dojo.require("esri.map");
      
      var map;
      
      function init() {
        map = new esri.Map("map",{
          basemap:"topo",
          center:[-122.45,37.75], //long, lat
          zoom:13,
          sliderStyle:"small"
        });
      }
      function getCenter(){
        var centerLocation = map.extent.getCenter();
        console.log(centerLocation.getLatitude());
        console.log(centerLocation.getLongitude());
      }
      dojo.ready(init);
    </script>

0 Kudos
ReneRubalcava
Frequent Contributor
If you want the lat/lng
esri.geometry.webMercatorToGeographic(map.extent.getCenter());

http://developers.arcgis.com/en/javascript/jsapi/namespace_geometry.html#webmercatortogeographic
This is assuming your map is in web mercator.
0 Kudos
JohnGravois
Frequent Contributor
shameless plug for the little utility app i wrote to help define map constructor options..

(because it demonstrates what you need to do, and just in case thats why you need the info :))
0 Kudos
BenFousek
Occasional Contributor III
shameless plug for the little utility app i wrote to help define map constructor options..


Thanks John. Added to my list of helpful jsapi links.
0 Kudos