Esri / arcgis-experience-builder-sdk-resources

ArcGIS Experience Builder samples
https://developers.arcgis.com/experience-builder/
Apache License 2.0
129 stars 121 forks source link

Fix get-map-coordinates props #199

Closed lillapulay-esri closed 1 day ago

lillapulay-esri commented 3 days ago

The get-map-coordinates tutorial features code snippets in order to display the cursor's coordinates when hovering over the map.

After following the steps, I ran into an error (Uncaught TypeError: Cannot read properties of null (reading 'toFixed')), so the lat/long values couldn't be displayed either. The 'completed widget' solution, that can be downloaded from the tutorial page, features the same code snippet:

/** ADD: **/
const activeViewChangeHandler = (jmv: JimuMapView) => {
  if (jmv) {
    // When the pointer moves, take the pointer location and create a Point
    // Geometry out of it (`view.toMap(...)`), then update the state.
    jmv.view.on('pointer-move', (evt) => {
      const point: Point = jmv.view.toMap({
        x: evt.x,
        y: evt.y
      })
      setLatitude(point.latitude.toFixed(3))
      setLongitude(point.longitude.toFixed(3))
    })
  }
}

The changes in this PR should fix this error by using the correct properties to update the state, so the tutorial's outcome is functional.

blissvisitor commented 2 days ago

The original code for obtaining latitude and longitude is fine, x and y are just screen coordinates; you can check if the point is right. https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-Point.html#properties-summary

lillapulay-esri commented 2 days ago

@blissvisitor Thank you for your answer!

For me the latitude and longitude of the point (and of jmv.view.center) is always null, only x an y have non-null values. I think since setLatitude and setLongitude use latitude and longitude instead of x and y, these will always have a null value.

According to the link you sent, which of these values can be used depends on the spatial reference - I'm new to this topic, so the only thing I did was following the tutorial, but just with those steps alone I can't get this to work, unless I change the code the way I did in this PR (plus, as I only just noticed, activeViewChangeHandler should also be edited for the map's extent to be changeable).

So do you mean the code/solution for the above mentioned tutorial works fine for you? For me it just displays that error and the placeholder text next to lat/lon. Please let me know, would like to understand it!

blissvisitor commented 1 day ago

It works fine for me, Please check your environment, note that this widget requires the selection of a Map Widget.

https://github.com/user-attachments/assets/ea104025-ecd1-4c90-983b-f4c88da39cfc

lillapulay-esri commented 1 day ago

Thank you for your time! I'll close this PR then, and try to figure out why it's not working for me.