2013-08-23 36 views

回答

1

一種選擇是將其發送到的DirectionsRenderer之前修改在響應中的字符串:

function calcRoute() { 
    var request = { 
    origin: 'Brisbane QLD Australia Australia', 
    // origin: 'Brisbane Qld, Australia', 
    destination: '1 Morwong Drive, Noosa Heads Qld, Australia', 
    // waypoints:[{location: 'Bourke, NSW'}, {location: 'Broken Hill, NSW'}], 
    travelMode: google.maps.DirectionsTravelMode.DRIVING, 
    unitSystem: google.maps.UnitSystem.METRIC 
    }; 
    directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
     var route = response.routes[0]; 
     var lastleg = route.legs[route.legs.length-1]; 
     response.routes[0].legs[route.legs.length-1].end_address = 'No. 1 in Hastings Street'; 
     directionsDisplay.setDirections(response); 
    } else alert("Directions request failed: "+status); 
    }); 
} 

來處理情況,方向是由的DirectionsRenderer(即用於拖動原點)改變的情況下,你可以做這樣的事情(其中changeEnd是一個全球性的,並設置爲false),請注意,它會有不希望的結果,如果用戶拖動目標(可能要防止:Google maps api v3 Two markers one fixed one dragged):

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() { 
    computeTotalDistance(directionsDisplay.directions); 
    if (!changeEnd) { 
    var response = directionsDisplay.getDirections(); 
    var route = response.routes[0]; 
    var lastleg = route.legs[route.legs.length-1]; 
    response.routes[0].legs[route.legs.length-1].end_address = 'No. 1 in Hastings Street'; 
    changeEnd = true; 
    directionsDisplay.setDirections(response); 
    } else changeEnd = false; 
}); 
+0

完美的作品如果從中選擇一個起點下拉列表。如果有人將A標記拖到不同的地方,它會顯示街道地址,而不是「否」。 1在黑斯廷斯街'。 –

+0

您是否嘗試添加代碼來處理這種情況?關閉可拖動的方向可能會更好。 – geocodezip

+0

或類似的[這,只允許一個可拖動的標記](http://stackoverflow.com/questions/16966766/google-maps-api-v3-two-markers-one-fixed-one-dragged/16972214#16972214 ) – geocodezip