我有一個想法。它看起來很有效,至少對於我選擇類似情況的路點而言。
我把腿(=航點之間的路線段)放在for-loop中。對於每個分段,我計算/讀取「沿路線的距離」並將其除以航跡距離。這個比例應該是大約1.如果它大於1,那麼這個比例是rong。我把閾值設置爲2.0,你應該降低它(1到2之間的值是一個值)。
找到這樣一個因素後,我刪除該航點(當然,我在沒有這個項目的情況下創建一個新數組「newWaypoints」),然後重新開始,在2秒setTimeout後產生一個紅色路線。
函數是遞歸的,我不知道應該刪除多個點會發生什麼。也許它可能會再次嘗試一次。
<style>
#map {height: 400px;}
</style>
<div id="map"></div>
<div id="log"></div>
<input type="button" value="click" onclick="calc()">
<script>
var directionsDisplay;
var directionsService;
var map;
var maxFactor = 2.0; // FEEL FREE TO GUESTIMATE ANOTHER VALUE
function initMap() {
var start = new google.maps.LatLng(50.96622130043278,3.8518730520809185);
var mapOptions = {
zoom:7,
center: start
}
map = new google.maps.Map(document.getElementById('map'), mapOptions);
directionsService = new google.maps.DirectionsService();
}
function calcRoute(start, end, waypoints, color) {
directionsDisplay = new google.maps.DirectionsRenderer({ map: map, polylineOptions: {strokeColor: color} });
var request = {
origin: start,
destination: end,
waypoints: waypoints,
provideRouteAlternatives: false,
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.IMPERIAL
};
directionsService.route(request, function(result, status) {
if (status == 'OK') {
var newWaypoints = [];
directionsDisplay.setDirections(result);
var legs = result.routes[0].legs;
var problemIndex = -1;
for(var i in legs) {
var routeSegment = legs[i].distance.value;
var origin = legs[i].start_location;
var destination = legs[i].end_location;
var flightpathDistance = google.maps.geometry.spherical.computeDistanceBetween(origin, destination);
var factor = (routeSegment/flightpathDistance);
if(factor > maxFactor && problemIndex == -1) {
// too long
problemIndex = i;
document.getElementById('log').innerHTML += 'factor ' + factor.toFixed(2) + ' - segment looks too long. romove waypoint ' + i + ' (0 based)<br/>';
}
else if(factor > maxFactor && problemIndex == i -1) {
if(i<legs.length - 1) { // skip the destination; this is not a waypoint
newWaypoints.push(waypoints[i]);
}
document.getElementById('log').innerHTML += 'factor ' + factor.toFixed(2) + ' - segment also too long, but the problem is probably the previous segment<br/>';
}
else {
if(i<legs.length - 1) { // skip the destination; this is not a waypoint
newWaypoints.push(waypoints[i]);
}
document.getElementById('log').innerHTML += 'factor ' + factor.toFixed(2) + '<br/>';
}
}
document.getElementById('log').innerHTML += '<hr/>';
if(problemIndex > -1) {
setTimeout(function() {
calcRoute(start, end, newWaypoints, 'red');
}, 2000);
}
}
});
}
function calc() {
calcRoute(
'50.95487921891042,3.879781222422025',
'51.002379049703315,3.757394492495223',
[
{location: '50.96622130043278,3.8518730520809185', stopover: true},
{location: '50.9725522849737,3.8379914402503345', stopover: true},
{location: '50.97957292616706,3.8236401199901593', stopover: true}, // This is the problem point, on the opposite side of the road
{location: '50.98570531465853,3.81125807762146', stopover: true},
{location: '50.98941308813775,3.8019619700935436', stopover: true}
],
"blue"
);
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&libraries=geometry"></script>
我相信用於資產跟蹤的目的[道路API(https://developers.google.com/maps/documentation/roads/intro)可能比地圖API更適合。然而,道路API中使用的捕捉算法假設GPS點非常接近(小於400米)。以2分鐘的間隔,我恐怕點會稀疏,插值算法將無法正常工作。 – xomena
似乎它應該幫助。我會盡力。據我所知,除了Directions API之外,我還可以使用這個API。因此,我可以使用Roads API「修改」這些點並使用Directions API繪製路線。 –
沒有幫助:(看起來效果很好,只適用於點距離太近的情況,在我的情況下,兩點之間可能有2-10英里的距離 –