[英]How to get the distance of a route using Bing maps API?
The "function" that I need is in this link , but there is not a code example, just "RouteSummary Object" and the bellow there is the "distance", which is what I need.我需要的“功能”在此链接中,但没有代码示例,只有“RouteSummary Object”,下面是“距离”,这正是我需要的。
I got the code bellow in this link我在这个链接中得到了下面的代码
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
</head>
<body>
<div id='printoutPanel'></div>
<div id='myMap' style='width: 100vw; height: 100vh;'></div>
<script type='text/javascript'>
function loadMapScenario() {
var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
/* No need to set credentials if already passed in URL */
center: new Microsoft.Maps.Location(47.606209, -122.332071),
zoom: 12
});
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function () {
var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
// Set Route Mode to driving
directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
var waypoint1 = new Microsoft.Maps.Directions.Waypoint({ address: 'Redmond', location: new Microsoft.Maps.Location(47.67683029174805, -122.1099624633789) });
var waypoint2 = new Microsoft.Maps.Directions.Waypoint({ address: 'Seattle', location: new Microsoft.Maps.Location(47.59977722167969, -122.33458709716797) });
directionsManager.addWaypoint(waypoint1);
directionsManager.addWaypoint(waypoint2);
// Set the element in which the itinerary will be rendered
directionsManager.calculateDirections();
});
}
</script>
<script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?key=AnOeSZ6a7LgcnBLVjmZ4xfQWIWZjzKv7bDwgyRk-W4NlkGnZGQdk5atUjsunU5yH&callback=loadMapScenario' async defer></script>
</body>
</html>
I tried like that ways:我试过这样的方式:
routeSummary.distance();
and too:还有:
directionsManager.routeSummary.distance();
I don't know almost anything about JavaScript and don't know how to read documentation.我对 JavaScript 几乎一无所知,也不知道如何阅读文档。
after all, how do I use an Object like described in the documentation?毕竟,我如何使用文档中描述的对象? I just want to know what mean by "object" in this case, is the class-related object of OOP or not?
我只是想知道在这种情况下“对象”是什么意思,是不是OOP的类相关对象?
You need to add an event to the directions manager since the route calculation is done asynchronously.您需要向路线管理器添加一个事件,因为路线计算是异步完成的。 Here is an example:
下面是一个例子:
Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', directionsUpdated);
function directionsUpdated(e) {
//Get the current route index.
var routeIdx = directionsManager.getRequestOptions().routeIndex;
//Get the distance of the route.
var distance = .routeSummary[routeIdx].distance;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.