基本办法是利用谷歌地图的标语绘制地图中两个任意街道点之间的路线。 这些要点必须与其LatLng坐标编码。 这里就是一个法典例子(如APICV2所示):
function initMap() { // called by page onload event
if (GBrowserIsCompatible()) {
// set the initial location
gMap = new GMap2(document.getElementById("map_canvas"));
gMap.addControl(new GLargeMapControl());
gMap.setCenter(GLatLng(49.238326, 6.977761), 15);
// init directions object and attach listener to handle route loads from function highliteRoute()
gDir = new GDirections();
gPoly = null;
GEvent.addListener(gDir, load , function(){
gPoly = gDir.getPolyline();
gMap.addOverlay(gPoly);
// zoom & pan to poly
var polyBds = gPoly.getBounds();
var polyZoom = gMap.getBoundsZoomLevel(polyBds);
gMap.setZoom(polyZoom);
gMap.panTo(polyBds.getCenter());
});
}
}
function highliteRoute(){
if(gPoly!=null) gPoly.hide();
gDir.load( from: 49.313530,6.969109 to: 49.238326,6.977761 , {getPolyline:true});
}