我有一张地图,上面有一条包含一系列路点的路线。我希望能够通过点击地图添加新的标记,并使用以前的和新的标记自动计算路线,同时考虑optimizeWaypoints的设置:true。
我试过了,但不知道怎么做。
有人知道怎么做吗?
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: -32.949798, lng: -60.681911}
});
var directionsService = new google.maps.DirectionsService;
var directionsRenderer = new google.maps.DirectionsRenderer({
draggable: true,
map: map,
panel: document.getElementById('right-panel')
});
directionsRenderer.addListener('directions_changed', function() {
computeTotalDistance(directionsRenderer.getDirections());
});
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
}
displayRoute(new google.maps.LatLng(-32.949798, -60.681911), new google.maps.LatLng(-32.949798, -60.681911), directionsService,
directionsRenderer);
}
function displayRoute(origin, destination, service, display) {
service.route({
origin: origin,
destination: destination,
waypoints: [{location: new google.maps.LatLng(-32.930572, -60.662137)},
{location: new google.maps.LatLng(-32.923554, -60.665930)},
{location: new google.maps.LatLng(-32.936270, -60.652841
)},],
optimizeWaypoints: true,
travelMode: 'DRIVING',
avoidTolls: true,
avoidHighways: true
}, function(response, status) {
if (status === 'OK') {
display.setDirections(response);
} else {
alert('Could not display directions due to: ' + status);
}
});
}
function computeTotalDistance(result) {
var totalDistance = 0;
var totalDuration = 0;
var minutes = 0;
var hours = 0;
var myroute = result.routes[0];
for (var i = 0; i < myroute.legs.length; i++) {
totalDistance += myroute.legs[i].distance.value;
totalDuration += myroute.legs[i].duration.value;
}
totalDistance = totalDistance / 1000;
var hours = Math.floor( totalDuration / 3600 );
var minutes = Math.floor( (totalDuration % 3600) / 60 );
var seconds = totalDuration % 60;
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
if(hours == 0) {
var result = minutes + " min";
} else {
var result = hours + ':' + minutes + " hs";
}
document.getElementById('total_distance').innerHTML = totalDistance.toFixed(2) + ' km ';
document.getElementById('total_duration').innerHTML = result;
}
#right-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select, #right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
float: left;
width: 63%;
height: 100%;
}
#right-panel {
float: right;
width: 34%;
height: 100%;
}
.panel {
height: 100%;
overflow: auto;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Direcciones</title>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBMZqunCMWco2noEHk5BlWyJExOPn1XCRU&callback=initMap"></script>
</head>
<body>
<div id="map"></div>
<div id="right-panel">
<p>Distance: <span id="total_distance"></span></p>
<p>Duration: <span id="total_duration"></span></p>
</div>
</body>
</html>
- 点击地图时,将点击位置添加到"添加的路点"数组中
var addedWaypoints = [];
function addWaypoint(latLng) {
addedWaypoints.push(latLng);
}
- 再次呼叫方向服务,将新的航路点添加到请求中
google.maps.event.addListener(map, 'click', function(event) {
addWaypoint(event.latLng);
displayRoute(start, end, directionsService, directionsRenderer);
});
function displayRoute(origin, destination, service, display) {
var waypoints = [
{location: new google.maps.LatLng(-32.930572, -60.662137)},
{location: new google.maps.LatLng(-32.923554, -60.665930)},
{location: new google.maps.LatLng(-32.936270, -60.652841)}
];
for (var i=0; i<addedWaypoints.length;i++) {
waypoints.push({
location: addedWaypoints[i]
});
}
service.route({
origin: origin,
destination: destination,
waypoints: waypoints,
optimizeWaypoints: true,
travelMode: 'DRIVING',
avoidTolls: true,
avoidHighways: true
}, function(response, status) {
if (status === 'OK') {
display.setDirections(response);
} else {
alert('Could not display directions due to: ' + status);
}
});
}
概念验证小提琴
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {
lat: -32.949798,
lng: -60.681911
}
});
var directionsService = new google.maps.DirectionsService();
var directionsRenderer = new google.maps.DirectionsRenderer({
draggable: true,
map: map,
panel: document.getElementById('right-panel')
});
directionsRenderer.addListener('directions_changed', function() {
computeTotalDistance(directionsRenderer.getDirections());
});
var start = new google.maps.LatLng(-32.949798, -60.681911);
var end = new google.maps.LatLng(-32.949798, -60.681911);
google.maps.event.addListener(map, 'click', function(event) {
// placeMarker(event.latLng);
addWaypoint(event.latLng);
displayRoute(start, end, directionsService, directionsRenderer);
});
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
}
displayRoute(start, end, directionsService, directionsRenderer);
}
function displayRoute(origin, destination, service, display) {
var waypoints = [{
location: new google.maps.LatLng(-32.930572, -60.662137)
},
{
location: new google.maps.LatLng(-32.923554, -60.665930)
},
{
location: new google.maps.LatLng(-32.936270, -60.652841)
},
];
for (var i=0; i<addedWaypoints.length;i++) {
waypoints.push({
location: addedWaypoints[i]
});
}
var wayptsStr = "";
for (var i=0; i<waypoints.length; i++) {
wayptsStr += waypoints[i].location.toUrlValue(6)+",";
}
console.log("displayRoute: origin="+origin.toUrlValue(6)+" dest="+destination.toUrlValue(6)+" waypoints="+wayptsStr);
service.route({
origin: origin,
destination: destination,
waypoints: waypoints,
optimizeWaypoints: true,
travelMode: 'DRIVING',
avoidTolls: true,
avoidHighways: true
}, function(response, status) {
if (status === 'OK') {
display.setDirections(response);
} else {
alert('Could not display directions due to: ' + status);
}
});
}
var addedWaypoints = [];
function addWaypoint(latLng) {
addedWaypoints.push(latLng);
}
function computeTotalDistance(result) {
var totalDistance = 0;
var totalDuration = 0;
var minutes = 0;
var hours = 0;
var myroute = result.routes[0];
for (var i = 0; i < myroute.legs.length; i++) {
totalDistance += myroute.legs[i].distance.value;
totalDuration += myroute.legs[i].duration.value;
}
totalDistance = totalDistance / 1000;
var hours = Math.floor(totalDuration / 3600);
var minutes = Math.floor((totalDuration % 3600) / 60);
var seconds = totalDuration % 60;
hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
if (hours == 0) {
var result = minutes + " min";
} else {
var result = hours + ':' + minutes + " hs";
}
document.getElementById('total_distance').innerHTML = totalDistance.toFixed(2) + ' km ';
document.getElementById('total_duration').innerHTML = result;
}
#right-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select, #right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
float: left;
width: 63%;
height: 100%;
}
#right-panel {
float: right;
width: 34%;
height: 100%;
}
.panel {
height: 100%;
overflow: auto;
}
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBMZqunCMWco2noEHk5BlWyJExOPn1XCRU&callback=initMap"></script>
<div id="map"></div>
<div id="right-panel">
<p>Distance: <span id="total_distance"></span></p>
<p>Duration: <span id="total_duration"></span></p>
</div>