更改坐标时,不会创建航点

时间:2019-04-21 10:00:30

标签: javascript html node.js

我正在使用硬编码的坐标在两个标记之间的地图上创建航点。

但是它仅适用于单个坐标。当我更改坐标时,它没有显示新的航路点

<!DOCTYPE html><html><head><meta charset="utf-8"><title>Maps</title><style>
#map-canvas{
    height:700px;
    width:100%;
}
</style></head><body><div id= "map-canvas"></div><button id ="get">Get Directions</button><script>
function initMap(){
    var first1 = new google.maps.LatLng(27.721503,85.362072);
    var second1 = new google.maps.LatLng(27.711360,85.318781);
    var mapOptions = {
        zoom:6,
        center: first1
    };

    var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var directionsService = new google.maps.DirectionsService();
    directionsDisplay.setMap(map);

    function calculateRoute()
    {
        var request =
        {
            origin: first1,
            destination: second1,
            travelMode:'DRIVING'
        };
        directionsService.route(request,function(result,status) 
        {
            //console.log(result,status);

            if(status == "OK")
            {
                directionsDisplay.setDirections(result);
            }
        });
    }
   //  document.getElementById('get').onclick = function() {
        calculateRoute();
   //  }

}
</script><script async defer
src="https://maps.googleapis.com/maps/api/js?key=APIKEY&callback=initMap"></script></body></html>

预期:它也应与坐标一起工作

var first1 = new google.maps.LatLng(18.496668, 73.941666);
var second1 = new google.maps.LatLng(18.550985, 73.934982);

实际: 它仅适用于坐标

var first1 = new google.maps.LatLng(27.721503,85.362072);
var second1 = new google.maps.LatLng(27.711360,85.318781);

0 个答案:

没有答案