function NavigationTool() {
var mode = google.maps.DirectionsTravelMode.DRIVING; //谷歌地图路线指引的模式
var directionsDisplay = new google.maps.DirectionsRenderer(); //地图路线显示对象
var directionsService = new google.maps.DirectionsService(); //地图路线服务对象
var directionsVisible = false; //是否显示路线
directionsDisplay.setMap(null);
directionsDisplay.setMap(map);
var Navigpoints = []; //起终点
google.maps.event.addListener(map, "click", function(evt) {
if (Navigpoints.length == 0) {
if (confirm("是否将当前点设为起点?")) {
Navigpoints.push(evt.latLng);
}
} else {
if (Navigpoints.length >= 2) { points = []; google.maps.event.clearListeners(map, "click"); return; }
if (confirm("是否将当前点设为终点?")) {
Navigpoints.push(evt.latLng);
var request = {
origin: Navigpoints[0], //起点
destination: Navigpoints[1], //终点
travelMode: mode,
optimizeWaypoints: true,
avoidHighways: false,
avoidTolls: false
};
directionsService.route(
request,
function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
//定时清除线路
setTimeout(function() { directionsDisplay.setMap(null) }, 8000);
}
}
);
}
}
});
directionsVisible = true;
}