从Google Maps Directions API获取实际路线

时间:2014-10-05 02:00:09

标签: javascript google-maps google-maps-api-3

我正在使用Google Maps Directions API生成包含往返地点的路线的地图,但如何使用javascript API获取实际的文字说明?

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();    
var mapOfDirections;    
directionsDisplay = new google.maps.DirectionsRenderer();
var center = new google.maps.LatLng(lat, lon);
var mapOptions = {
    zoom:6,
    center: center
}
maps = new google.maps.Map(document.getElementById('main'), mapOptions);
directionsDisplay.setMap(maps);
var request = {
    origin:from,        
    destination:to,
    travelMode:google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status){
if(status==google.maps.DirectionsStatus.OK) directionsDisplay.setDirections(result);
});

1 个答案:

答案 0 :(得分:0)

这是我最终如何做到的:

function calculateDirections(){
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var directionsOptions = {
        panel:document.getElementById('directionsDiv')
    }
    directionsDisplay = new google.maps.DirectionsRenderer(directionsOptions);
    var request = {
        origin:from,    
        destination:to,
        travelMode:google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(result, status){
        if(status==google.maps.DirectionsStatus.OK) directionsDisplay.setDirections(result);        
    });
}