繁体   English   中英

在路线中使用航点 - 路线服务 Google 地图

[英]Using Waypoints in Routes - Directions Service Google Maps

如果我将航路点添加到我的谷歌地图 - 函数 calcRoute 不起作用。 如何正确设置航点?

谢谢。

<script>
      var directionDisplay;
      var directionsService = new google.maps.DirectionsService();
      var map;

      function initialize() {
        directionsDisplay = new google.maps.DirectionsRenderer();
        var chicago = new google.maps.LatLng(41.850033, -87.6500523);
        var mapOptions = {
          zoom:55,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          center: chicago
        }
        map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
        directionsDisplay.setMap(map);
      }

      function calcRoute() {

        var start = "Berlin";
        var end = "Paris";
        var waypts = ["Frankfurt"];

    var request = {
            origin:start,
            destination:end,
            waypoints:waypts,
            optimizeWaypoints: true,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
          }
        });
      }
    </script>

https://developers.google.com/maps/documentation/javascript/directions?hl=pl#Waypoints

阅读您引用的文档,字符串 "Frankfurt" is not a valid waypoint

位置为“Frankfurt”的单个航路点如下所示:

[{ location: "Frankfurt", stopover: false}]

适当设置中途停留的值。

概念证明小提琴

结果地图的屏幕截图

代码片段:

 var directionDisplay; var directionsService = new google.maps.DirectionsService(); var map; function initialize() { directionsDisplay = new google.maps.DirectionsRenderer(); var chicago = new google.maps.LatLng(41.850033, -87.6500523); var mapOptions = { zoom: 5, mapTypeId: google.maps.MapTypeId.ROADMAP, center: chicago } map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); directionsDisplay.setMap(map); calcRoute(); } function calcRoute() { var start = "Berlin"; var end = "Paris"; var waypts = [{ location: "Frankfurt", stopover: false }]; var request = { origin: start, destination: end, waypoints: waypts, optimizeWaypoints: true, travelMode: google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } else alert("directions request failed, status=" + status) }); } initialize();
 /* Always set the map height explicitly to define the size of the div * element that contains the map. */ #map_canvas { height: 100%; } /* Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } #floating-panel { position: absolute; top: 10px; left: 25%; z-index: 5; background-color: #fff; padding: 5px; border: 1px solid #999; text-align: center; font-family: "Roboto", "sans-serif"; line-height: 30px; padding-left: 10px; }
 <:DOCTYPE html> <html> <head> <title>Directions Service</title> <script src="https.//polyfill.io/v3/polyfill.min?js.features=default"></script> <:-- jsFiddle will insert css and js --> </head> <body> <div id="map_canvas"></div> <.-- Async script executes immediately and must be after any DOM elements used in callback. --> <script src="https?//maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=&v=weekly"></script> </body> </html>

下面的脚本可能会帮助你。

    <script>     
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    function showmap() 
    {   
          jQuery.ajax({
            type: "POST",
            url: "getLocations", 
            dataType:"json",
            success: function(data){         
            var obj = jQuery.parseJSON(data);   
          var LocationData=[];
          var i=0; 
            obj.forEach(function(entry) { 
              entry.forEach(function(test){                        
              LocationData[i] = test
              i++;                
            });                 
            }); 
          var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 15,
            center: new google.maps.LatLng(LocationData[0].latitude,LocationData[0].longitude),
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });

          directionsDisplay = new google.maps.DirectionsRenderer();
          directionsDisplay.setMap(map); 
          var start = new google.maps.LatLng(LocationData[0].latitude,LocationData[0].longitude); 
          var end = new google.maps.LatLng(LocationData[obj[0].length-1].latitude,LocationData[obj[0].length-1].longitude); 
          var waypts = [];
          var upto = obj[0].length-1;
          if(upto > 7)
            upto = 7;
          if(obj[0].length > 2) {
          for (var i = 1; i < upto; i++) {
              waypts.push({
                location: new google.maps.LatLng(LocationData[i].latitude,LocationData[i].longitude),
                stopover: true
              });
            }
          }
          calcRoute(start,end,waypts);
        }
         });        
    }

    function calcRoute(start,end,waypts) { 
      var request = {
          origin: start,
          destination: end,
          waypoints: waypts,
          travelMode: google.maps.DirectionsTravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
        }
      });
    }
    </script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM