添加标记到路线

时间:2013-06-23 23:13:05

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

我知道如何根据坐标添加标记,但有没有办法将其添加到route

例如,“在离芝加哥100米的路线上添加标记”。

var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();

var myOptions = {
    zoom:7,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map(document.getElementById("map"), myOptions);
directionsDisplay.setMap(map);

var request = {
    origin: 'Chicago', 
    destination: 'New York',
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {

        console.log(response.routes[0].legs[0]);

        // Display the distance:
        document.getElementById('distance').innerHTML += 
            response.routes[0].legs[0].distance.value + " meters";

        // Display the duration:
        document.getElementById('duration').innerHTML += 
            response.routes[0].legs[0].duration.value + " seconds";

        directionsDisplay.setDirections(response);
    }
});

http://jsfiddle.net/fDwDY/

0 个答案:

没有答案