Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 谷歌地图多段线:点击多段线的部分并返回ID?_Google Maps_Google Maps Api 3_Polyline - Fatal编程技术网

Google maps 谷歌地图多段线:点击多段线的部分并返回ID?

Google maps 谷歌地图多段线:点击多段线的部分并返回ID?,google-maps,google-maps-api-3,polyline,Google Maps,Google Maps Api 3,Polyline,我有一个谷歌地图V3多段线。我可以在整个多段线上检测单击事件,但我可以对单击事件执行更高级的操作吗 我想做的是检测多段线的哪个部分被单击,并在警报中显示 routePath = new google.maps.Polyline({ path: routeCoordinates, strokeColor: "#CC33FF", strokeWeight: 3 }); routePath.setMap(map); google.maps.event.a

我有一个谷歌地图V3多段线。我可以在整个多段线上检测单击事件,但我可以对单击事件执行更高级的操作吗

我想做的是检测多段线的哪个部分被单击,并在警报中显示

 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?
 });
有人知道如何在谷歌地图上做到这一点吗


谢谢

在单击事件上,您可以收到单击坐标的板条。但是,由于这可能不是创建多段线的精确点,因此需要找到最近的点。您可以使用谷歌地图库中的ComputedIstanceBeween,也可以使用毕达哥拉斯定理,因为在这种情况下,毕达哥拉斯定理应该能提供足够的精度

您可以在此处找到有关computeDistanceBetween的更多信息:

下面是一个代码示例,说明如何使用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.maps.event.addListener(路由路径,'click',函数(h){
var latlng=h.latlng;
警报(routePath);
var针={
距离:9999999999,//傻高
索引:-1,
latlng:null
};
routePath.getPath().forEach(函数(routePoint,索引){
var dist=google.maps.geometry.spheremic.ComputedDistanceBetween(latlng,routePoint);
if(距离<针距){
针距=距离;
针头指数=指数;
指针.latlng=路由点;
}
});
//多段线中最近的点
警报(“最近的索引:+指针索引”);
//多段线上单击的点
警报(latlng);
});

在许多情况下,通过距离分析找到最近的点会失败,因为路径会越过或靠近自身


您可以使用它来识别候选线,但您应该通过比较创建的两条线的和/或来确认它们。如果您使用单击点分割两个连续的多段线点,我遇到了相同的问题,这里是我如何处理它的: 设置处理程序时:

google.maps.event.addListener(routePath, 'click', function(e) {
    handlePolyClick(e, this)
});

var handlePolyClick(eventArgs, polyLine) {
    // now you can access the polyLine
    alert(polyLine.strokeColor);
});
或者,如果要访问相关对象,请通过在多段线上创建变量对其进行设置:

routePath.car = $.extend({}, cars[1]); // shallow copy of cars[1]
然后,您可以从活动中访问您的汽车:

alert(this.car.color);

这不会告诉您单击了多段线的哪个部分。。。是吗?我,我试着像你一样直接在多段线上添加元素,我做错了什么?