How do I get the lat and lon from the center of mapview

498
3
07-13-2023 09:52 AM
JulianBorrego
New Contributor

I'm displaying a mapview and want to get the latitude and longitude of the center, how can I do this?

 

0 Kudos
3 Replies
MikeWilburn
Esri Contributor

Hi Julian,

Try this workflow:
1. Get the map view's visibleArea,
2. Retrieve the extent envelope of that polygon,
3. Get the center point of envelope,
4. Access x & y

Cheers,
Mike

0 Kudos
JulianBorrego
New Contributor

So I tried that but I am getting really weird numbers: I have the map set for 

latitude = -34.0, longitude = 151.0

but when I print the log for lat and lon using

val lat = binding.map.visibleArea?.extent?.center?.x!!
val lon = binding.map.visibleArea?.extent?.center?.y!!

i get lat: 1.680924310978431E7 , lon:-4028802.026134409

0 Kudos
MikeWilburn
Esri Contributor

Looks like you're getting back coordinates in meters (likely from a Web Mercator spatial reference set on the map).

Two options:

  • use the CoordinateFormatter to convert those into a string of lat/long
  • use getCurrentViewpoint to get the viewpoint in lat/long by default:
val viewpoint = binding.mapView.getCurrentViewpoint(ViewpointType.CenterAndScale)
val latLongPoint = viewpoint?.targetGeometry?.extent?.center

 

0 Kudos