gpt4 book ai didi

google-maps - 方向api : check if a place falls in the route path between 2 places

转载 作者:行者123 更新时间:2023-12-04 01:05:50 25 4
gpt4 key购买 nike

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

这是我从代码中生成的路由路径的快照。

Route map between A and 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);
}
});
});
}

我该怎么办?

最佳答案

您可以使用几何库(您可以通过将脚本 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 点是否“在路上”实际上是一个棘手的概念 - 离路多远以至于不能“在途中”?

关于google-maps - 方向api : check if a place falls in the route path between 2 places,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com