简体   繁体   English

如何找到用户单击折线的位置?

[英]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.

相关问题 如何检测用户是否点击了 react-multi-carousel 中的箭头? - How to detect if the user has clicked on the arrows in react-multi-carousel? 如何跟踪和显示所有用户点击按钮的总次数? - How can I keep track of and display the amount of times a button has been clicked in total by all users? 我该如何呈现使用facebook login登录的用户名? - How can I render the name of the user that has logged in with facebook login? 当使用 JWT 单击某个链接时,如何重定向用户登录 - How can I redirect a user to login when a certain link is clicked using JWT 我怎样才能让“其他”变成一个按钮,用户可以在其中输入任意数量的钱? - How can I have where the 'other' turns into a button where the user can input as much money as they want? 如何查询它只会显示具有今天日期字段时间戳的文档? - How can I query where it will only show the documents where it has a field timestamp of today's date? 如何查找和过滤属于用户的博客? - How can i find and filter the blogs that belong to a user? 如何在 React 中单击鼠标的位置动态添加 div? - How do I dynamically add a div where mouse was clicked in React? 在哪里可以找到 meteor js 的插件? - Where can I find plugins for meteor js? 我在哪里可以找到 CRA 中的 .eslintrc? - Where can I find .eslintrc in CRA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM