Bug(Polygon) : onMouseUp event returns wrong coordinates when editing Polygon edge
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
onMouseUpaccessible on theGoogleMapcomponent 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
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
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.
@mathieu-anderson do you found a workaround?
@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.
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 ..

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 please provide minimal reproduction in codesandbox.io
@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 please check fixed version https://codesandbox.io/s/heuristic-snow-92lyi?file=/src/App.js
@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