方向api:检查一个地方是否落入两个地方之间的路径路径 [英] Direction api: check if a place falls in the route path between 2 places

查看:102
本文介绍了方向api:检查一个地方是否落入两个地方之间的路径路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google方向API来绘制2个地点A和B之间的路径路径。我可以做到这一点。现在,我需要检查给定的地点C落在A和B的路径路径中。



下面是我生成的路径路径的快照从我的代码。





以下是相应的代码:

 函数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);
}
});
});
}

我该如何去做?
<您可以使用几何库(您可以通过将脚本src更改为 https://maps.googleapis来请求Google地图。 com / maps / api / js?sensor = false& libraries = geometry )并使用 isLocationOnEdge 并使用 LatLng 点C和从 DirectionsService 返回的折线。



https://developers.google.com/maps/documentation/javascript/geometry#isLocationOnEdge 然后,如果你将它添加为航点,那么点C可以 ALWAYS 在A和B之间的路上,因此确定点C在路上实际上是一个棘手的概念 - 有多远,因为它不能在路上太远?


I am using Google Direction API to plot the route path between 2 places A and B. I am able to do this. Now, I have a requirement to check if given a place C falls in the route path of A and B.

Here is the snapshot of the route path that I have generated from my code.

Here is the corresponding code:

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);
                        }
                    });
                });
            }

How can I go about it?

解决方案

You can use the geometry library that (you can request with google maps by changing your script src to https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry) and use isLocationOnEdge and use the LatLng of point C and the polyline that is returned from the DirectionsService.

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

Then again, point C could ALWAYS be on the way between A and B if you added it as a waypoint, so determining if point C is "on the way" is actually a bit of a tricky concept - how far out of the way is too far for it to not be "on the way"?

这篇关于方向api:检查一个地方是否落入两个地方之间的路径路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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