Google地图折线:点击折线部分并返回ID? [英] Google Maps polyline: Click on section of polyline and return ID?

查看:157
本文介绍了Google地图折线:点击折线部分并返回ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有Google Maps V3折线。我可以检测整个折线上的点击事件,但是我可以使用点击事件做更高级的事情吗?



我想要做的是检测哪一部分折线已被点击,并显示在警报中。

  routePath = new google.maps.Polyline({
路径:routeCoordinates,
strokeColor:#CC33FF,
strokeWeight:3
});
routePath.setMap(map);
google.maps.event.addListener(routePath,'click',function(){
alert(routePath);
// TODO:显示折线的哪一部分已被点击?
});

有人知道如何在Google Maps中执行此操作吗?



感谢!

解决方案

在点击事件上,您可以收到被点击的坐标的LatLng。但是,因为这可能不是创建多段线的确切点,所以您需要找到最近的点。您可以在Google Maps库中使用computeDistanceBetween,或者您可以使用Pythagoras定理,因为在这种情况下,它应该能够提供足够的准确性。



有关更多信息, computeDistanceBetween here:
https://developers.google.com/maps / documentation / javascript / reference#spherical

下面是一个代码示例,介绍如何使用computeDistanceBetween实现它。

  google.maps.event.addListener(routePath,'click',function(h){
var latlng = h.latLng;
alert(routePath);
var needle = {
minDistance:9999999999,//傻傻的高
索引:-1,
latlng:null
};
routePath.getPath()。forEach(function(routePoint,index){
var dist = google.maps.geometry.spherical.computeDistanceBetween(latlng,routePoint);
如果(dist needle.minDistance = dist;
needle.index = index;
needle.latlng = routePoint;
}
} );
//多段线中的最近点
alert(最近的索引:+ needle.index);

//多段线上的单击点
alert(latlng);

});


I have a Google Maps V3 polyline. I can detect click events on the entire polyline, but can I do anything more advanced with the click event?

What I'd like to do is detect which section of the polyline has been clicked, and show this in an alert.

 routePath = new google.maps.Polyline({
     path: routeCoordinates,
     strokeColor: "#CC33FF",
     strokeWeight: 3
 });     
 routePath.setMap(map);
 google.maps.event.addListener(routePath, 'click', function() {
     alert(routePath);
     // TODO: display which section of the polyline has been clicked?
 });

Does anyone know how to do this in Google Maps?

thanks!

解决方案

On the click event you can receive a LatLng of the coordinate that was clicked. However, since that will probably not be an exact point that is creating the polyline you need to find the closest point. You can use the computeDistanceBetween in the Google Maps library or you can use Pythagoras theorem as it should give you a good enough accuracy in this case.

You can find more information on computeDistanceBetween here: https://developers.google.com/maps/documentation/javascript/reference#spherical

Here is a code example how you could do it with the computeDistanceBetween.

google.maps.event.addListener(routePath, 'click', function(h) {
     var latlng=h.latLng;
     alert(routePath);
     var needle = {
         minDistance: 9999999999, //silly high
         index: -1,
         latlng: null
     };
     routePath.getPath().forEach(function(routePoint, index){
         var dist = google.maps.geometry.spherical.computeDistanceBetween(latlng, routePoint);
         if (dist < needle.minDistance){
            needle.minDistance = dist;
            needle.index = index;
            needle.latlng = routePoint;
         }
     });
     // The closest point in the polyline
     alert("Closest index: " + needle.index);

     // The clicked point on the polyline
     alert(latlng);

 });

这篇关于Google地图折线:点击折线部分并返回ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆