这是我一直使用的代码:
它在地图上显示路线,在#info div 中显示方向信息
<form id="route">
<label>Address: <input type="text" id="addr" /></label>
<button id="route">Calculate route</button>
</form>
<div id="map_canvas"></div>
<div id="info"></div>
$(function(){
var center = new google.maps.LatLng(32.498467,74.542126);
var map = new google.maps.Map(document.getElementById("map_canvas"),{
center: center,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var m = new google.maps.Marker({map:map,position:center});
var dD = new google.maps.DirectionsRenderer({map:map,panel:$('#info')[0]});
$('#route').submit(function(ev){
var addr = $('#addr').val();
var ds = new google.maps.DirectionsService();
ds.route({
origin:addr,
destination:center,
region:'at',
travelMode: google.maps.TravelMode.DRIVING
},function(result,status){
// if(status == google.maps.DirectionsStatus.OK){
dD.setDirections(result);
$('#info').fadeIn(200);
// }
});
ev.preventDefault();
});
});