[英]How can I find where a user has clicked on a Polyline?
If I add a Polyline to my map with a click function:如果我通过单击 function 将折线添加到我的 map:
onPolylineClick = (props, polyline) => {
console.log(props)
console.log(polyline)
}
<Polyline
onClick={this.onPolylineClick}
path={gpsCoords}
/>
Then how do I figure out where on the line it was clicked?那么我如何确定它被点击的位置呢? I can see
props.mapCenter
but that doesn't change depending on where I click, it's just the centre of the map.我可以看到
props.mapCenter
但这不会根据我单击的位置而改变,它只是 map 的中心。
You need to also pass a third parameter in your onPolylineClick since the latLng for the point you clicked in the polyline is there.您还需要在 onPolylineClick 中传递第三个参数,因为您在折线中单击的点的 latLng 就在那里。 You can do something like:
您可以执行以下操作:
onPolylineClick = (props, polyline, e) => {
console.log("Polyline Clicked on latlng: ")
console.log(e.latLng.lat()+","+e.latLng.lng());
};
You can check this sample code .您可以查看此示例代码。 Make sure to add your API Key in the GoogleApiWrapper for the code to properly run.
确保在 GoogleApiWrapper 中添加您的 API 密钥,以使代码正确运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.