// in the global scope
var directions = [];
document.getElementById('submit').addEventListener('click', function() {
if (directions && directions.length > 0) {
for (var i=0; i<directions.length; i++)
directions[i].setMap(null);
}
directions = [];
calculateAndDisplayRoute(directionsService, markerArray, stepDisplay, map, true, "SUBWAY");
});
function calculateAndDisplayRoute(directionsService,markerArray, stepDisplay, map, is_transit, transit_mode) {
//var selectedMode = "TRANSIT";
// First, remove any existing markers from the map.
for (var i = 0; i < markerArray.length; i++) {
markerArray[i].setMap(null);
}
if (is_transit == true){
var request = {
origin: {lat: start_lat, lng: start_lon},
destination: {lat: end_lat, lng: end_lon},
travelMode: google.maps.TravelMode.TRANSIT,
transitOptions: {
modes: [google.maps.TransitMode[transit_mode]],
},
provideRouteAlternatives: true
};
}else{
var request = {
origin: {lat: start_lat, lng: start_lon},
destination: {lat: end_lat, lng: end_lon},
travelMode: google.maps.TravelMode[transit_mode],
provideRouteAlternatives: true
};
}
// Retrieve the start and end locations and create a DirectionsRequest using
directionsService.route(request, function(response, status) {
// Route the directions and pass the response to a function to create
console.log(response)
console.log(response.routes[0])
var polyline = new google.maps.Polyline({
strokeColor: '#6855C9',
strokeOpacity: 1,
strokeWeight: 7
});
if (status == google.maps.DirectionsStatus.OK) {
for (var i = 0, len = response.routes.length; i < len; i++) {
directions.push(new google.maps.DirectionsRenderer({
map: map,
directions: response,
routeIndex: i ,
suppressMarkers: true
}));
//showSteps(response, markerArray, stepDisplay, map);
}
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
그랬습니다. 정말 고마워! – Jim
이 솔루션을 기반으로 대체 경로를 그려 보면 어떻게 할 수 있습니까? http://stackoverflow.com/questions/18974512/how-to-display-alternative-route-using-google-map-api 이 솔루션 각 경로의 루프에 매번 새로운 DirectionsRenderer를 생성하므로 위의 솔루션을 사용할 수 없습니다. – beta