多种出行模式,使用谷歌地图 API 渲染一张地图
Multiple travel modes to render one map using google maps API
我有两种不同的旅行类型。由于两种不同的旅行类型,我使用了 3 个请求,并为每个请求指定了旅行类型。
App.directionsService.route(startLeg, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
App.directionsDisplay1.setDirections(result);
}
});
App.directionsService.route(middleLeg, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
App.directionsDisplay2.setDirections(result);
}
});
App.directionsService.route(endLeg, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
App.directionsDisplay3.setDirections(result);
}
是否可以在地图上渲染所有三组结果?
根据要求,使用多个 DirectionsRenderer-实例的示例(AFAIK 对使用的实例没有限制):
var goo = google.maps,
map = new goo.Map(document.getElementById('map-canvas'), {
center: new goo.LatLng(52.52, 13.40),
zoom: 10
}),
App = {
map: map,
bounds: new goo.LatLngBounds(),
directionsService: new goo.DirectionsService(),
directionsDisplay1: new goo.DirectionsRenderer({
map: map,
preserveViewport: true,
polylineOptions: {
strokeColor: 'red'
}
}),
directionsDisplay2: new goo.DirectionsRenderer({
map: map,
preserveViewport: true,
polylineOptions: {
strokeColor: 'blue'
}
}),
directionsDisplay3: new goo.DirectionsRenderer({
map: map,
preserveViewport: true,
polylineOptions: {
strokeColor: 'yellow'
}
})
},
startLeg = {
origin: 'Rome',
destination: 'Paris',
travelMode: goo.TravelMode.DRIVING
},
middleLeg = {
origin: 'Paris',
destination: 'Berlin',
travelMode: goo.TravelMode.TRANSIT
},
endLeg = {
origin: 'Berlin',
destination: 'Buxtehude',
travelMode: goo.TravelMode.BICYCLING
};
App.directionsService.route(startLeg, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
App.directionsDisplay1.setDirections(result);
App.map.fitBounds(App.bounds.union(result.routes[0].bounds));
}
});
App.directionsService.route(middleLeg, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
App.directionsDisplay2.setDirections(result);
App.map.fitBounds(App.bounds.union(result.routes[0].bounds));
}
});
App.directionsService.route(endLeg, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
App.directionsDisplay3.setDirections(result);
App.map.fitBounds(App.bounds.union(result.routes[0].bounds));
}
});
演示(包括说明面板):http://jsfiddle.net/doktormolle/y6xEP/
注意:我的示例使用地址,它不会导致连续路由。要获得带有地址的连续路由,您必须逐个发送请求,并使用上一个响应的 destination-LatLng 作为下一个请求的源
相关文章:
- 有人能提供一张ember数据与broswer's的持久层
- jQuery只隐藏<tr>在一张桌子上
- 如何从三张图片中选择一张?其余的必须是看不见的
- 通过onclick事件将一张图片更改为6张图片
- Sequelize hasMany浏览另一张表
- jm按下停止步骤进入滚动的下一张幻灯片
- 我用JavaScript FIleReader读了一张图片,但是我无法得到图片的信息
- 当 Flickity 到达下一张或上一张幻灯片时的事件
- 单击从一张图像切换到两张图像,然后再切换回一张图像
- 用d3和topojson绘制一张地图
- 附近搜索的标记仅显示在最后一张地图上
- 一张 Ovi 地图上有 2 条或更多路线
- 根据标记在另一张地图中的拖动来更新地图中心
- Esri Javascript API人口普查信息弹出和公园弹出一张地图
- 在一张地图上显示多个谷歌地图标记和方向
- 我怎样才能拥有一张地图<字符串,integer>在javaScript中
- 我试图在一个给定地址的网站上加载多个谷歌地图.最后一张地图是唯一显示的地图
- 在谷歌地图上放一张图片
- 多种出行模式,使用谷歌地图 API 渲染一张地图
- 我如何使用算法随机生成一张“地图”?使用预设图像