找出LngLat点是否在两个其他点之间

时间:2013-12-05 12:41:32

标签: javascript google-maps-api-3 vector latitude-longitude

我正在使用谷歌地图,并试图找出一个点是否在两个其他点之间。到目前为止,我一直在做的方式是制作两个向量。一个在两个“检查”点之间,一个在“检查”点之间,另一个在“新”点之间。在计算了向量之后,我已经越过了它们,并得到了一个交叉值。 然后我对我所有的“检查”点都做了同样的事情,如果新的crossProduct小于旧的那个,我坐的把它换掉了。 这一直在努力,但现在我遇到了无法解决的麻烦。所以我正在寻找另一个公式,以确定该点是否在其他两个点之间,并有误差。

希望你能帮我解决这个问题

1 个答案:

答案 0 :(得分:2)

在原始两点之间构造折线。然后使用isLocationOnEdge函数确定第3个点是否与折线相差一定的度数。

e.g。

<!DOCTYPE html>
<html>
<head>
<title>Is a point within x degrees of a polyline</title>

<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { width:100%; height:100%; }
</style>
<!-- need to load the geometry library -->
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false"></script>

<script type="text/javascript">
    function initialize() {
        var latlng = new google.maps.LatLng(54.5003526, -3.0844116);

        var myOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        var marker = new google.maps.Marker({
            position: latlng,
            map: map, 
            title: "foo"
        });

        var latLng1 = new google.maps.LatLng(54.60039, -3.13632);
        var latLng2 = new google.maps.LatLng(54.36897, -3.07561);

        var polyline = new google.maps.Polyline({
            path: [latLng1, latLng2],
            strokeColor: "#FF0000",
            strokeOpacity: 1.0,
            strokeWeight: 2,
            map: map
        });

        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 1));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.1));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.01));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.001));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.0001));
        console.log(google.maps.geometry.poly.isLocationOnEdge(latlng, polyline, 0.00001));
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

</head>
<body>
    <div id="map_canvas"></div>
</body>
</html>

这样做我可以看到我的点距离我的线在0.1度以内,但它不在0.01度之内。您的学位公差将根据具体情况而有所不同。