2

How to get map center lat long in arcgis javascript api?

I looked the api reference of 'Map' object, only see map.getZoom()

There is NO such method map.getCenter()

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 23, 2018 at 22:44

1 Answer 1

4

On google map api v3, map is the google map object, this is how you get map center lat, long, and zoom level.

center_latLng = map.getCenter();
center_lat = center_latLng.lat();
center_long = center_latLng.lng();
center_zoom = map.getZoom();

However, arcgis api is little different, map is arcgis map object, you will use extend property.

 center_latLng = map.extent.getCenter();
 center_lat = center_latLng.getLatitude();
 center_long = center_latLng.getLongitude();
 center_zoom = map.getZoom();

Usually, when you get current map lat, long, zoom level, you want to update the current web page query URL, which has old info before map extend change.

if ('URLSearchParams' in window) {
 var searchParams = new URLSearchParams(window.location.search);
 searchParams.set("center_lat", center_lat);
 searchParams.set("center_long", center_long);
 searchParams.set("center_zoom", center_zoom);
 // this cause reload https://stackoverflow.com/questions/5999118/how-can-i-add-or-update-a-query-string-parameter
 //window.location.search = searchParams.toString();
 // instead avoid reload
 var newRelativePathQuery = window.location.pathname + '?' + searchParams.toString();
 history.pushState(null, '', newRelativePathQuery);
 }// if

So the whole function is:

function update_center_latLngZoom(){
 center_latLng = map.extent.getCenter();
 center_lat = center_latLng.getLatitude();
 center_long = center_latLng.getLongitude();
 center_zoom = map.getZoom();
 if ('URLSearchParams' in window) {
 var searchParams = new URLSearchParams(window.location.search);
 searchParams.set("center_lat", center_lat);
 searchParams.set("center_long", center_long);
 searchParams.set("center_zoom", center_zoom);
 // this cause reload https://stackoverflow.com/questions/5999118/how-can-i-add-or-update-a-query-string-parameter
 //window.location.search = searchParams.toString();
 // instead avoid reload
 var newRelativePathQuery = window.location.pathname + '?' + searchParams.toString();
 history.pushState(null, '', newRelativePathQuery);
 }// if
}// function

This function should be trigged by map move or zoom or extend change. You need to attach this function to map extend change event.

1) google map, attach to map idle event

 map.addListener('idle', function() { 
 update_center_latLngZoom();
 });

2)arcgis map api, attach to map extent-change event

 map.on("extent-change", function(){
 // url search parameter update 
 update_center_latLngZoom()
 });
answered May 23, 2018 at 22:48
1
  • Well done! You can always get the Center of any geometry Object, for example the Extend. A normal Object, as a Map, doesn't have an explicit geometry. Commented May 24, 2018 at 13:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.