谷歌地图方向 mvcobject 获得单独的步骤
Google Maps directions mvcobject getting a individual step
我正在使用directionsService通过google maps javascript api获取路线。问题是我想获取单个步骤,但将它们作为 mvc 对象的一部分,因此当您单击它们时,它们会在地图上呈现为默认值
directionsDisplay.setDirections(response);
确实如此。一些代码:
var directionsService = new google.maps.DirectionsService();
directionsDisplay.setMap(map);
var request = {
origin : a,
destination : b,
travelMode : google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
//Instead of setting directions to a panel i want to
//set just one to a panel and yet have it link to the
//map like the above does.
}});
这个想法是你必须迭代路线的每一条路,然后遍历这条路的每一步,并渲染你自己的HTML来表示它。请参阅以下代码(假设您定义了 map
、directionsDisplay
和 directionsService
以及request
):
directionsService.route(request, function(response, status) {
// Make sure the directions request was valid
if (status == google.maps.DirectionsStatus.OK) {
// Give the response to the directions display
directionsDisplay.setDirections(response);
// Display the route on the map
directionsDisplay.setMap(map);
// Grab the result from the routes
var routeResult = response.routes[0];
// Iterate over each leg of the route
routeResult.legs.forEach(function(leg) {
// Build HTML for each step
var stepHTML = '';
leg.steps.forEach(function(step) {
stepHTML += '<div class="directionStep">'+
'<div>'+step.instructions+'</div>'+
'<div class="grey">'+step.distance.text+' ('+step.duration.text+')'+'</div>'+
'</div>';
});
// Put the step HTML somewhere
$('.steps').append(stepHTML);
});
}
});
然后,聆听各个步骤的点击,并做任何您需要的响应。
相关文章:
- ng映射方向备选方案
- 在单独的ul's
- Javascript排序的图像弹出窗口..可以't单独弹出
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- RubyonRails——构建交互式接口应该朝哪个方向发展
- 需要帮助谷歌地图方向面板在FancyBox中显示
- 在D3.js中,有没有任何方法可以将x和y方向上的滚动事件绑定到平移svg
- 将单独的数组深层键转换为所需的类型(数组或对象)
- 从单独的html文件预览Javascript文本
- 有什么“;错误的”;将javascript放在asp.net表单上,而不是放在单独的文件中
- 如何将要单独填充的每个HTML画布路径/形状分开
- 如何使jQuery中的悬停函数单独应用于数据库映像
- 如何在foreach循环中对每个产品单独应用评级系统
- 我怎样才能知道用户滑动的方向
- Javascript单独的游戏窗口
- 当两个单独的单词被放在目标上时,使用Jquery获取一个值
- drawimage到一个单独的画布宽度和高度
- 有人能为我指明正确的方向,让一个javascript文件在另一个单独的javascript文件之后运行吗
- 谷歌地图方向 mvcobject 获得单独的步骤
- 在请求中放置单独的方向API键