方向api:检查一个地方是否落在2个地方之间的路线中

Ada*_*ady 12 google-maps google-maps-api-3 google-directions-api google-places-api

我正在使用Google Direction API绘制2个地点A和B之间的路径路径.我能够这样做.现在,我需要检查一个地方C是否落在A和B的路径中.

这是我从代码生成的路径路径的快照.

A和B之间的路线图

这是相应的代码:

function initialize() {
                var input = document.getElementById('searchTextFieldSource');
                var input1 = document.getElementById('searchTextFieldDestination');

                var autocomplete = new google.maps.places.Autocomplete(input);
                var autocomplete1 = new google.maps.places.Autocomplete(input1);
                google.maps.event.addListener(autocomplete1, 'place_changed', function () {
                    var place = autocomplete.getPlace();
                    document.getElementById('city1').value = place.name;
                    var place1Lat = place.geometry.location.lat();
                    var place1Lng = place.geometry.location.lng();
                    document.getElementById('cityLat1').value = place1Lat;
                    document.getElementById('cityLng1').value = place1Lng;

                    var obj = new Object();
                    obj.city =place.name;
                    obj.latitude = place.geometry.location.lat();
                    obj.longitude = place.geometry.location.lng();
                    locations.push(obj);


                    var place2 = autocomplete1.getPlace();
                    document.getElementById('city2').value = place2.name;
                    var place2Lat = place2.geometry.location.lat();
                    var place2Lng = place2.geometry.location.lng();
                    document.getElementById('cityLat2').value = place2Lat;
                    document.getElementById('cityLng2').value = place2Lng;

                    var obj = new Object();
                    obj.city = place2.name;
                    obj.latitude = place2.geometry.location.lat();
                    obj.longitude = place2.geometry.location.lng();
                    locations.push(obj);

                    directionsDisplay = new google.maps.DirectionsRenderer();
                    var startPlace = new google.maps.LatLng(place1Lat, place1Lng);

                    var mapOptions = {
                        zoom:7,
                        center: startPlace
                    }

                    var map = new google.maps.Map(document.getElementById('map'), mapOptions);
                    directionsDisplay.setMap(map);
                    //refreshMap(locations);

                    var start = $("#city1").val();
                    var end = $("#city2").val();
                    var request = {
                          origin:start,
                          destination:end,
                          travelMode: google.maps.TravelMode.DRIVING
                    };
                    directionsService.route(request, function(response, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                          directionsDisplay.setDirections(response);
                        }
                    });
                });
            }
Run Code Online (Sandbox Code Playgroud)

我该怎么办呢?

Ada*_*dam 9

您可以使用几何库(您可以通过更改脚本src来请求谷歌地图https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry)并使用isLocationOnEdge和使用LatLng点C和从中返回的折线DirectionsService.

https://developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge

再说一次,如果你把它作为一个航路点添加,C点总是可以在A和B之间的路上,所以确定C点是否"在路上"实际上是一个棘手的概念 - 走多远的路是太远了它不能"在路上"?