use*_*587 1 javascript google-maps-api-3
我有一个有三种不同旅行类型的三腿旅行.由于两种不同的旅行类型,我使用了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);
}
Run Code Online (Sandbox Code Playgroud)
是否可以在地图上渲染所有三组结果?
根据请求使用多个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));
}
});
Run Code Online (Sandbox Code Playgroud)
演示(包括说明面板):http://jsfiddle.net/doktormolle/y6xEP/
注意:我的示例使用地址,它不会产生连续路线.要获得具有地址的连续路线,您必须逐个发送请求,并使用上一个响应的destination-LatLng作为下一个请求的来源
| 归档时间: |
|
| 查看次数: |
7089 次 |
| 最近记录: |