how to get map center point?

20223
9
06-18-2014 04:16 AM
Aaronsi
New Contributor
I use map.extent.getCenter()  to get the map center point.But the result is wrong?
I also couldn't find any method of Map Object can get the map center point.
I'm new in Arcgis. I hope someone could help me?
Thanks!
0 Kudos
9 Replies
LuciHawkins
Occasional Contributor III
Hi,

Might need a little more information.  You received output but the "result was wrong"?   What projection are you using?  Did you set the spatialreference for the desired coordinate output?

Thanks,

Luci
0 Kudos
Aaronsi
New Contributor
Hi,

Might need a little more information.  You received output but the "result was wrong"?   What projection are you using?  Did you set the spatialreference for the desired coordinate output?

Thanks,

Luci


my javascript code are :

var map;

require(["esri/map",], function(Map){

map = new Map("map", {
          basemap: "satellite",
          center: [-46.807, 32.553],
          zoom: 3
        });

});

function getCenterPoint()
{
return map.extent.getCenter();
}

function showCenterPoint()
{
var point = getCenterPoint();

console.log("current map center is x: " + point.x + ", y: " + point.y);
}

the result that I expect is : -46.807, 32.553;
but the real result is : current map center is x: -5376858.37910917, y: 3836121.3860294037
Thanks!
0 Kudos
KenBuja
MVP Esteemed Contributor
The map's projection is in Web Mercator, so the x and y of the center will be returned in meters. You'll want to project this to geographic coordinates, like this

        require([
          "esri/map", "esri/geometry/webMercatorUtils", "dojo/domReady!"
        ], function (
          Map, webMercatorUtils
        ) {
            map = new Map("map", {
                basemap: "satellite",
                center: [-46.807, 32.553],
                zoom: 3
            });

            function getCenterPoint() {
                return map.extent.getCenter();
            }

            map.on("extent-change", function () {
                var point = getCenterPoint();

                var newPoint = webMercatorUtils.webMercatorToGeographic(point);
                console.log("current map center is x: " + newPoint.x + ", y: " + newPoint.y);
            });

        });
0 Kudos
BenFousek
Occasional Contributor III
You can also
map.extent.getCenter().getLatitude();
map.extent.getCenter().getLongitude();
in a web mercator map.
Aaronsi
New Contributor
Hi, Kenbuja, Btfou,
    
After I changed my code like that:

var map;

require(["esri/map", "esri/geometry/webMercatorUtils", "dojo/domReady!"], function(Map, webMercatorUtils){

map = new Map("map", {
          basemap: "satellite",
          center: [-46.807, 32.553],
          zoom: 3
        });
 
function getCenterPoint()
{
  return map.extent.getCenter();
}

map.on("extent-change", function(){
 
  var point = getCenterPoint();
 
  var newPoint = webMercatorUtils.webMercatorToGeographic(point);
 
  console.log("current map center point is x: " + point.getLatitude() + ", y: " + point.getLongitude());
  console.log("current map center is x: " + newPoint.x + ", y: " + newPoint.y);
});

});

That worked out perfectly!

Thanks very much!
0 Kudos
KenBuja
MVP Esteemed Contributor
Glad to help.

Please click the check mark in the appropriate post to mark the question as answered. This helps other when searching on solutions to questions like this. For more information, see read the MVP Program page.
0 Kudos
Aaronsi
New Contributor

Hi, Ken Buja

    

     Good to hear from you!  Actually I would like to click the check mark, but the problem is that I couldn't find where the check mark is?  Any help?

0 Kudos
KenBuja
MVP Esteemed Contributor

In the new forums, the procedure to mark a question as the answer has been changed. Normally, it should be clear how to mark a question as answered in the new forums. However, this thread was begun in the old forum, and when it was brought over to the new forum, it was classified a discussion and not a question, which means it doesn't have the option to mark it as answered. Hopefully this will be changed soon as they continue to work on the migration process.

Aaronsi
New Contributor

Hi Ken Buja Thanks for you reply for me. As you said that means I wasn't able to mark it as answered right now. so I don't need to mark it at the moment, right?

0 Kudos