在路线中使用航点 - 方向服务谷歌地图

时间:2012-10-29 22:02:57

标签: javascript 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

2 个答案:

答案 0 :(得分:1)

阅读您引用的文档,字符串“Frankfurt”不是有效的waypoint

位于“法兰克福”的单个航点将如下所示:

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

适当设置中途停留值。

答案 1 :(得分:0)

下面的脚本可能会对您有所帮助。

    <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>