dynamically set mapview.center

796
5
04-28-2020 05:22 AM
KoenGallet
New Contributor

Hi,

trying to get mapview centered around a changing set of coordinates, in other words the mapview moves and the (changing) coordinate pair stays centered.

I start by building the map and center it around an initial coordinate pair. Immediately after that I launch a periodical GET request (intervals of 200ms) to a server who responds with updated coordinate pairs, which I then assign to mapview.center . When I console.log(view.center) I can see the values in the center object changing over time. So far , everything seems to be working as intended. But the mapview does not change. It stays centered around the initial pair of coordinates and , curious thing, when I manually drag the mapview to another location, one that movement is finished, the mapview jumps back to the initial coordinates. can someone point out to me what I am misunderstanding about how to approach this?

Thank you very much

Koen

0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Koen,

   The best approach is to use the view.goTo method.

0 Kudos
KoenGallet
New Contributor

Hi Robert, thank you for your swift reply. I had tried goTo. Have now tried it again. Didn't work. In the browser console every variable I send out reflects the changes, but the mapview stays where it is...

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Koen,

   Sounds like you may be trying to set the map to coordinates that are not the same as the views spatial Reference then. Can you share some code?

KoenGallet
New Contributor

Robert,

about the spatial reference and sharing some code:

This is where I initially set up the map and thereafter the periodical GETrequest and goTo

cheers,

Koen

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Koen,

   If you are still having issues then the better approach to this is to create an actual Point class with a specified spatial reference to give to the view.goTo target or center property.

var centerPnt = new Point(newcoords, new SpatialReference({ wkid:4326 }));
view.goTo({center: centerPnt});