react-google-maps-api icon indicating copy to clipboard operation
react-google-maps-api copied to clipboard

Bug(Polygon) : onMouseUp event returns wrong coordinates when editing Polygon edge

Open mathieu-anderson opened this issue 7 years ago • 8 comments

Please provide an explanation

onMouseUp event provides wrong latLng as part of its returned value when editing a Polygon's edges.

How does it behave?

Codesanbox of issue : https://codesandbox.io/s/reactgooglemapsapi-polygon-edge-onmouseup-bug-5916w

To reproduce :

  • click on an edge (midpoint between two points of the Polygon)
  • drag to a new position
  • let go
  • the point added to the path is not the same as the one under the mouse when the event fired

Desired behaviour

Always get the coordinates under the mouse when onMouseUp is fired.

Alternative solutions :

  • being able to hide the midway edge points
  • having onMouseUp accessible on the GoogleMap component to track these kind of event more globally

If this is due to incorrect usage of the library's API, I am sorry, and I hope you will be able to explain my mistakes to me.

Your Environment

Provided in Codesandbox

Locally : os: MacOS Mojave 10.14.5 node v12.1.0 react ^16.8.4 webpack ^4.28.4 @babel ^7.2.2 @react-google-maps/api 1.2.3

mathieu-anderson avatar May 29 '19 11:05 mathieu-anderson

I have looked into an issue and forked your bug example, and I can confirm that there is a bug in google-maps-api. If you can look at my example, there is correct coordinates returned by onMouseUp on component.

https://codesandbox.io/s/react-google-maps-api-polygon-edge-onmouseup-bug-93x7e

I hope you can hack around your issue, but at the same time I would recommend you to create an issue at google maps api issue tracker and link this issue there.

JustFly1984 avatar Jun 11 '19 23:06 JustFly1984

@mathieu-anderson do you found a workaround?

JustFly1984 avatar Jul 16 '19 08:07 JustFly1984

@JustFly1984 I actually have not delved deeper into the issue, as the solution proposed in the Spectrum chat and shared here actually fit the need I had.

The issue is still there on v1.4.2 of react-google-maps, as far as I can tell. But if there is 100% certainty that it is an issue with Google and not the library, this issue can probably be closed.

mathieu-anderson avatar Jul 16 '19 09:07 mathieu-anderson

I don't know if this is the problem from google or the library (react-google-maps-api) but I have seen a solution that is the center-point symmetry method and still works for me ..

Untitled Diagram

const getListPosition = (event, index) => {
    let currentIndex = event.vertex || event.edge
    let polygonData = elRefs[index].current.props.path.map(polygon => ({ lng: polygon.lng, lat: polygon.lat }))
    if (event.edge == 0 || event.edge) {
      currentIndex = event.edge + 1
      let newLat = event.latLng.lat() * 2 - polygonData[currentIndex - 1].lat
      let newlng = event.latLng.lng() * 2 - polygonData[currentIndex - 1].lng
      polygonData.splice(currentIndex, 0, { lng: newlng, lat: newLat });
    }
    if (event.vertex == 0 || event.vertex) {
      currentIndex = event.vertex
      polygonData[currentIndex] = { lng: event.latLng.lng(), lat: event.latLng.lat() }
    }
    if (elRefs[index].current.props.path.length > 0) {
      let polygonDataReturn = polygonData.map(polygon => [polygon.lng, polygon.lat])
      getListPolygon(polygonDataReturn, index)
    }
  }
 <Polygon
            ref={elRefs[index]}
            key={index + "enable"}
            onMouseUp={(event) => getListPosition(event, index)}
            options={PolygonOption}
            path={polygonDefault.map(point => ({ lat: point[1], lng: point[0] }))}
          />```

huynv3 avatar Jan 02 '21 04:01 huynv3

@huynv3 please provide minimal reproduction in codesandbox.io

JustFly1984 avatar Jan 02 '21 05:01 JustFly1984

@huynv3 please provide minimal reproduction in codesandbox.io

https://codesandbox.io/s/bold-lehmann-ibiv0?fontsize=14&hidenavigation=1&theme=dark you can check my minimal project here

huynv3 avatar Jan 02 '21 08:01 huynv3

@huynv3 please check fixed version https://codesandbox.io/s/heuristic-snow-92lyi?file=/src/App.js

JustFly1984 avatar Jan 02 '21 08:01 JustFly1984

@JustFly1984 if i have multiple polygons in format of [lat , long][] the one i am editing remains on maps while order gets removed when i move vertex of one

nahmad-cc avatar Dec 18 '23 10:12 nahmad-cc