点击了哪一段折线? [英] Which segment of a polyline was clicked?

查看:121
本文介绍了点击了哪一段折线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个具有五个细分受众群的示例折线,并且允许使用新标记在用户点击多段线时创建。



我想知道是否有一种可靠的方法来确定新标记是否位于标记0和1之间,或者1和2 ...或者4和5之间。我已经考虑过检查新标记是否在边界框内,以及线内公式,但都不是100%准确。


由于Google地图使用墨卡托投影

a>用于投影GPS坐标,因为gps坐标的投影不是线性的,所以不能使用'直线方程',而是可以使用世界坐标,其中是线性的。在这里,我使用参数形式的直线方程来检查段上的

<$函数isPointOnSegment(map,gpsPoint1,gpsPoint2,gpsPoint){
var p1 = map.getProjection()。fromLatLngToPoint(gpsPoint1); p $ p>
var p2 = map.getProjection()。fromLatLngToPoint(gpsPoint2);
var p = map.getProjection()。fromLatLngToPoint(gpsPoint);
var t_x;
var t_y;
//线性方程的参数形式为:
// ------------------------------- -
// x = x1 + t(x2-x1)
// y = y1 + t(y2-y1)
// ----------- ---------------------
//'p'在[p1,p2]段上,如果't'是从[0,1 ]
// -----案例1 ----
// x = x1
// y = y1
// -------- -------
if(p2.x-p1.x == 0&& p2.y-p1.y == 0){
return px == p1.x &安培;&安培; p.y == p1.y;
} else
// -----案例2 ----
// x = x1
// y = y1 + t(y2-y1)
// ---------------
if(p2.x-p1.x == 0&& p2.y-p1.y!= 0) {
t_y =(py - p1.y)/(p2.y-p1.y);
返回p.x == p1.x&& t_y> = 0&& t_y <= 1;
} else
// -----案例3 ----
// x = x1 + t(x2-x1)
// y = y1
// ---------------
if(p2.x-p1.x!= 0&& p2.y-p1.y == 0) {
t_x =(px - p1.x)/(p2.x-p1.x);
返回p.y == p1.y&& t_x> = 0&& t_x <= 1;
}
// -----案例4 ----
// x = x1 + t(x2-x1)
// y = y1 + t( y2-y1)
// ---------------
t_x =(px - p1.x)/(p2.x-p1.x);
t_y =(p.y - p1.y)/(p2.y-p1.y);
return(t_x == t_y& t_x> = 0&& t_x< = 1&< t_y> = 0& t_y< = 1);
}

通过单击点和所有折线段,可以使用上面实现的功能并检索您正在查找的细分。


I have set up a sample polyline with five segments, and I'm allowing new markers to be created when the user clicks on the polyline.

I'd like to know if there's a foolproof way to determine whether the new marker is between markers 0 and 1, or 1 and 2 ... or between 4 and 5. I've considered checking if the new marker is inside a bounding box, and point-in-line formulas, but neither is 100% precise.

解决方案

As Google Maps uses Mercator Projection for projecting GPS coordinates, you can't use 'line equation' for gps coordinates, because projection is not linear for gps points.But instead you can use world coordinates which are linear. Here I have used parametric form of line equation to check whether point on segment:

function isPointOnSegment( map, gpsPoint1, gpsPoint2, gpsPoint ){
     var p1 = map.getProjection().fromLatLngToPoint( gpsPoint1 );
     var p2 = map.getProjection().fromLatLngToPoint( gpsPoint2 );
     var p = map.getProjection().fromLatLngToPoint( gpsPoint );
     var t_x;
     var t_y;
     //Parametric form of line equation is:
     //--------------------------------
     //      x = x1 + t(x2-x1)
     //      y = y1 + t(y2-y1) 
     //--------------------------------
     //'p' is on [p1,p2] segment,if 't' is number from [0,1]
     //-----Case 1----
     //      x = x1
     //      y = y1
     //---------------
     if( p2.x-p1.x == 0 && p2.y-p1.y == 0){
        return p.x == p1.x && p.y == p1.y;
     }else 
     //-----Case 2----
     //      x = x1
     //      y = y1 + t(y2-y1)
     //---------------
     if( p2.x-p1.x == 0 && p2.y-p1.y != 0){
        t_y = (p.y - p1.y)/(p2.y-p1.y);
        return p.x == p1.x && t_y >= 0 && t_y <= 1;
     }else
     //-----Case 3----
     //      x = x1 + t(x2-x1)
     //      y = y1 
     //---------------
     if( p2.x-p1.x != 0 && p2.y-p1.y == 0){
        t_x = (p.x - p1.x)/(p2.x-p1.x);
        return p.y == p1.y && t_x >= 0 && t_x <= 1;
     }
     //-----Case 4----
     //      x = x1 + t(x2-x1)
     //      y = y1 + t(y2-y1) 
     //---------------
     t_x = (p.x - p1.x)/(p2.x-p1.x);
     t_y = (p.y - p1.y)/(p2.y-p1.y);
     return ( t_x == t_y && t_x >= 0 && t_x <= 1 && t_y >= 0 && t_y <= 1);
} 

By having clicked point and all segments of polyline, you could use above implemented function and retrieve segment you were looking for.

这篇关于点击了哪一段折线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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