gpt4 book ai didi

javascript - 如何添加新标记并计算新路线

转载 作者:行者123 更新时间:2023-12-03 16:26:29 26 4
gpt4 key购买 nike

我有一张包含一系列航点的路线的 map 。我希望能够通过点击 map 来添加新的标记,并使用以前和新的标记自动计算路线,同时考虑到 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>

最佳答案

  • 当您单击 map 时,将单击的位置添加到“添加的航点”数组中。


  • 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);
    }
    });
    }

    proof of concept fiddle

    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>

    关于javascript - 如何添加新标记并计算新路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60290195/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com