viewof map coordinates on click

There is something I don’t understand. I’m trying to get the coordinates of my cursor from an svg map. It’s OK in the console. But I don’t have anything in output. What is the right way to do this?
Find here a draft unlisted notebook: Draft / neocarto / Observable

1 Like

I have a widget like that on one of my notebooks.

I think you have to generate an input event when the coordinate is set. For an HTML element this is something like div.dispatchEvent(new CustomEvent("input")) (where the console.log is), but I’m not sure whether you can do this with an SVG.

@neocarto You need to make two changes. First, you need to dispatch “input” events to let Observable’s Runtime know that your data has changed:

svg.on("click", function (ev) {
    // ...

You’re also currently reassigning coords, which means that your initially assigned value becomes stale after the first update. Instead define a getter:

  return Object.defineProperty(svg.node(), "value", {
    get: () => coords

I recommend to take a look at this short introduction to viewof:

1 Like

Thanks :+1: