2

I am using Arcgis javascript API for my map where map is coming from arcgis online using map by ID. Now I want to set map center using Lat long,For this I am using this method to set map center

   map.centerAt(42.58,78.25);

But this method is not working for me. Does anybody know how to set map center using Lat Long In Arcgis javascript API.

6

the centerAt() method expects a point geometry object.

//pass long,lat and don't forget to load the appropriate AMD module
map.centerAt(new Point(-118.15, 33.80));
//or
map.centerAt(new Point(-118.15, 33.80, new SpatialReference(wkid: 4326));

this is mentioned in the API reference: https://developers.arcgis.com/javascript/jsapi/map-amd.html#centerat

the object can be constructed using WGS84 coordinates without specifying a spatial reference, but it doesn't hurt anything to include it.

https://developers.arcgis.com/javascript/jsapi/point-amd.html

2

Since my reputation is < 50 I can't add a comment to the answer provided, however, I would like to point out that second version of the map.centreAt does not work. It should be:

map.centerAt(new Point(-118.15, 33.80, new SpatialReference({wkid: 4326})));

Note the braces around wkid: 4326 this defines the object passed in to the SpatialReference constructor. Without that javascript assumes they are two parameters being passed in and should be separated with a comma.

There is also an extra closing parenthesis required.

0

Warning: you need to

      require(["esri/geometry/Point"], function(Point) { /* code goes here */ });

1) arcgis js api v3.28 only,

For example, I want map center at lng, lat with zoom level 18

Recommend:

map.centerAndZoom(new Point(lng, lat), 18);

Alternative: (sometimes, it failed to move the map, maybe ESRI need to fix bug for Javascript api v3.24)

map.centerAt(new Point(lng, lat));
//or
// map.centerAt(new Point(lng, lat, new SpatialReference({wkid: 4326})));
map.setZoom(18);

2) arcgis js v4.11 only

          view.center = [_center_long,  _center_lat]
       view.zoom = _center_zoom;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.