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.
2 Answers 2
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(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
-
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.Graham H– Graham H2016年11月10日 22:34:03 +00:00Commented Nov 10, 2016 at 22:34
-
It says Point is not defined. And when I add it to require and function statements, it says Point is not a constructor.Ε Г И І И О– Ε Г И І И О2020年03月13日 08:16:36 +00:00Commented Mar 13, 2020 at 8:16
-
1I'd willing to bet that it's because the order of the AMD modules you're importing doesn't match the order of the variable names you refer to them by in the callback.john gravois– john gravois2020年03月14日 14:20:06 +00:00Commented Mar 14, 2020 at 14:20
-
thanks for catching that typo. 4 years later, but I just fixed my snippet to just use a number.john gravois– john gravois2020年03月14日 14:21:22 +00:00Commented Mar 14, 2020 at 14:21
-
@JohnGravois you were damn right! It was just a mismatch between the two lists. Thank you!Ε Г И І И О– Ε Г И І И О2020年03月17日 13:26:15 +00:00Commented Mar 17, 2020 at 13:26
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;