将路点添加到谷歌距离矩阵中以确定里程
Adding waypoints to Google Distance Matrix to determine mileage
我在这里创建了一个简单的jsfiddle,使用谷歌的地图距离矩阵来确定2个点之间的里程。这很好,但后来我对确定2分以上的里程感兴趣。然而,我无法做到这一点,因为我认为我没有正确地实现它。有人知道如何将路点添加到谷歌的距离矩阵中,以确定他们之间的最佳汽车路线/里程吗?例如,如果我想通过一堆其他地方从The Underpass, Birmingham
到8 Shandon Close, Birmingham
。我的代码包含在下面:
var origin = "The Underpass, Marston Green, Birmingham, West Midlands B40 1PA, UK",
destinations = ["8 Shandon Close, Birmingham, West Midlands B32 3XB, UK","2 The Osiers, Elford, Tamworth, Staffordshire B79 9DG, UK","170 Bells Lane, Birmingham, West Midlands B14 5QA, UK","246 Long Acre, Birmingham, West Midlands B7 5JP, UK","28 Cuthbert Road, Birmingham, West Midlands B18 4AG, UK"]
service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [origin],
destinations: destinations,
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false,
unitSystem: google.maps.UnitSystem.IMPERIAL
},
callback);
function callback(response, status) {
var orig = document.getElementById("orig"),
dest = document.getElementById("dest"),
dist = document.getElementById("dist");
if (status == "OK") {
orig.value = response.originAddresses[0];
dest.value = response.destinationAddresses[0];
dist.value = response.rows[0].elements[0].distance.text;
} else {
alert("Error: " + status);
}
}
来自距离矩阵的文档
谷歌的Distance Matrix服务计算旅行距离和行程使用给定模式的多个起点和终点之间的持续时间长途行走
此服务不返回详细的路线信息。路线信息,可以通过传递所需的单一始发地和目的地。
如果您需要通过一组(少于8个)路点在2个地方之间的距离,请使用方向服务
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var mapOptions = {
zoom: 7,
center: chicago
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
calcRoute();
}
var origin = "The Underpass, Marston Green, Birmingham, West Midlands B40 1PA, UK",
destinations = ["8 Shandon Close, Birmingham, West Midlands B32 3XB, UK", "2 The Osiers, Elford, Tamworth, Staffordshire B79 9DG, UK", "170 Bells Lane, Birmingham, West Midlands B14 5QA, UK", "246 Long Acre, Birmingham, West Midlands B7 5JP, UK", "28 Cuthbert Road, Birmingham, West Midlands B18 4AG, UK"]
service = new google.maps.DistanceMatrixService();
function calcRoute() {
var waypts = [];
for (var i = 1; i < destinations.length - 1; i++) {
waypts.push({
location: destinations[i],
stopover: true
});
}
var request = {
origin: origin,
destination: destinations[destinations.length - 1],
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var orig = document.getElementById("orig"),
dest = document.getElementById("dest"),
dist = document.getElementById("dist");
orig.value = response.routes[0].legs[0].start_address;
dest.value = response.routes[0].legs[3].end_address;
var total_distance = 0.0;
for (var i=0; i<response.routes[0].legs.length; i++) {
total_distance += response.routes[0].legs[i].distance.value;
}
dist.value = total_distance +" meters";
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map-canvas"></div>
<div id="mileage-details">Origin:
<input id="orig" type="text" style="width:35em">
<br>
<br>Destination:
<input id="dest" type="text" style="width:35em">
<br>
<br>Distance:
<input id="dist" type="text" style="width:35em">
</div>
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 在函数中添加数组元素的数值
- 如何将歌曲添加到jPlayer
- 在html Select中添加搜索
- Java脚本时间添加
- FabricJs-限制主对象内添加对象的移动区域
- 将路点添加到谷歌距离矩阵中以确定里程
- Waypoint.js:当距离顶部N距离时添加类
- 如何添加节点到距离矩阵服务请求
- 向目的地可变距离矩阵API成功和失败添加目的地的纬度和长度