问题描述
我在制作出租车票价计算器。其中一项业务要求是,该公司希望获得最短和最快的路线选择。我知道谷歌directionService默认搜索最快的路线。我将请求参数中的avoidhighways选项设置为true以获得最短路由,但我对结果并不满意。任何人都有更好的解决方案为了获得从A到BI的最短路线,建议使用alternatives = true来进行不同的查询。
任何人都有更好的解决方案为了获得从A到BI的最短路线,建议使用alternatives = true来进行不同的查询。
参数,避免=收费,避免=高速公路之间的避免参数玩,然后我会比较所有结果以选择最短的路线。
directionsService = new google.maps.DirectionsService;
//避免收费
directionsService.route({
origin:{
'placeId':originId
},
destination:{
'placeId':destinationId
},
provideRouteAlternatives:true,
avoidTolls:true,
travelMode:google.maps.TravelMode.DRIVING
},function(response,状态){
if(status === google.maps.DirectionsStatus.OK){
routesResponses.push(response);
}
else {
window。警报(由于'+状态导致方向请求失败);
}
});
//避开高速公路
directionsService.route({
origin:{
'placeId':originId
},
destination:{
'placeId':destinationId
},
provideRouteAlternatives:true,
avoidHighways:true,
travelMode:google.maps.TravelMode.DRIVING
},function(response,状态){
if(status === google.maps.DirectionsStatus.OK){
routesResponses.push(response);
}
else {
window。 alert('Directions请求失败,原因是'+状态);
}
//结果分析和绘制路线
var faster = Number.MAX_VALUE,
最短= Number.MAX_VALUE;
r outesResponses.forEach(function(res){
res.routes.forEach(function(rou,index){
console.log(distance of route+ index +:,rou.legs [0 ] .distance.value);
console.log(路由持续时间+ index +:,rou.legs [0] .duration.value);
if(rou.legs [0] .distance.value< shortest)shortest = rou.legs [0] .distance.value;
if(rou.legs [0] .duration.value<最快)最快= rou.legs [0] .duration.value;
))
})
console.log(shortest:,最短);
console.log(最快:,最快);
//以绿色和红色绘制路线
routesResponses.forEach(function(res){
res.routes.forEach(function(rou,index){
new google .maps.DirectionsRenderer({
map:map,
directions:res,
routeIndex:index,
polylineOptions:{
strokeColor:rou.legs [0]。 duration.value ==最快?red:rou.legs [0] .distance.value ==最短?darkgreen:blue,
strokeOpacity:rou.legs [0] .duration.value = =最快?0.8:rou.legs [0] .distance.value ==最短?0.9:0.5,
strokeWeight:rou.legs [0] .duration.value ==最快?9:rou.legs [0 ] .distance.value == shortest?8:3,
}
})
})
})
});
}
}
I am making a taxi fare calculator. One of the business requirements is that, the company wants the shortest and fastest route options. I know Google directionService by default searched for the fastest route. I set the "avoidhighways" option in request parameter to true in order to get the shortest route, but I am not quite happy with the result.
Anyone have a better solution than that??
To obtain the shortest route from A to B I would suggest to make different queries with the "alternatives=true" parameter, playing with the "avoid" parameter between avoid=toll, avoid=highways, and then I would compare all results to pick the shortest route.
directionsService = new google.maps.DirectionsService;
//avoiding tolls
directionsService.route({
origin: {
'placeId': originId
},
destination: {
'placeId': destinationId
},
provideRouteAlternatives: true,
avoidTolls: true,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
routesResponses.push(response);
}
else {
window.alert('Directions request failed due to ' + status);
}
});
//avoiding highways
directionsService.route({
origin: {
'placeId': originId
},
destination: {
'placeId': destinationId
},
provideRouteAlternatives: true,
avoidHighways: true,
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
routesResponses.push(response);
}
else {
window.alert('Directions request failed due to ' + status);
}
//Results analysis and drawing of routes
var fastest = Number.MAX_VALUE,
shortest = Number.MAX_VALUE;
routesResponses.forEach(function(res) {
res.routes.forEach(function(rou, index) {
console.log("distance of route " +index+": " , rou.legs[0].distance.value);
console.log("duration of route " +index+": " , rou.legs[0].duration.value);
if (rou.legs[0].distance.value < shortest) shortest = rou.legs[0].distance.value ;
if (rou.legs[0].duration.value < fastest) fastest = rou.legs[0].duration.value ;
})
})
console.log("shortest: ", shortest);
console.log("fastest: ", fastest);
//painting the routes in green blue and red
routesResponses.forEach(function(res) {
res.routes.forEach(function(rou, index) {
new google.maps.DirectionsRenderer({
map:map,
directions:res,
routeIndex:index,
polylineOptions:{
strokeColor: rou.legs[0].duration.value == fastest? "red":rou.legs[0].distance.value == shortest?"darkgreen":"blue",
strokeOpacity: rou.legs[0].duration.value == fastest? 0.8:rou.legs[0].distance.value == shortest? 0.9: 0.5,
strokeWeight: rou.legs[0].duration.value == fastest? 9:rou.legs[0].distance.value == shortest? 8: 3,
}
})
})
})
});
}
}
这篇关于一种在Google Maps API v3中查找最短和最快路线的优雅方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!