An issue while showing a path on Google Maps

后端 未结 1 573
别那么骄傲
别那么骄傲 2021-01-27 12:40

I have multiple points of longitude and latitude. For example:

1 long lat, starting point   
2 long lat   
3 long lat     
4 long lat
.
.
.

. <

相关标签:
1条回答
  • 2021-01-27 13:07

    Don't push the src into the path, it is a duplicate of the first point in the overview path. Remove this line:

    path.push(src);
    

    working fiddle

    code snippet:

    var markers = [{
        "title": '1',
        "lat": '30.705911',
        "lng": '76.679656',
        "description": '1'
      }, {
        "title": '2',
        "lat": '30.701713',
        "lng": '76.684097',
        "description": '2'
      }, {
        "title": '2',
        "lat": '30.703291',
        "lng": ' 76.701022',
        "description": '2'
      }, {
        "title": '2',
        "lat": '30.691888',
        "lng": ' 76.710721',
        "description": '2'
      }
    
    ];
    window.onload = function() {
      var mapOptions = {
        center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
      var infoWindow = new google.maps.InfoWindow();
      var lat_lng = new Array();
      var latlngbounds = new google.maps.LatLngBounds();
      for (i = 0; i < markers.length; i++) {
        var data = markers[i]
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
        lat_lng.push(myLatlng);
        var marker = new google.maps.Marker({
          position: myLatlng,
          map: map,
          title: data.title
        });
        latlngbounds.extend(marker.position);
        (function(marker, data) {
          google.maps.event.addListener(marker, "click", function(e) {
            infoWindow.setContent(data.description);
            infoWindow.open(map, marker);
          });
        })(marker, data);
      }
      map.setCenter(latlngbounds.getCenter());
      map.fitBounds(latlngbounds);
    
      //***********ROUTING****************//
    
      //Intialize the Path Array
      var path = new google.maps.MVCArray();
    
      //Intialize the Direction Service
      var service = new google.maps.DirectionsService();
    
      //Set the Path Stroke Color
      var poly = new google.maps.Polyline({
        map: map,
        strokeColor: '#4986E7'
      });
    
      //Loop and Draw Path Route between the Points on MAP
      for (var i = 0; i < lat_lng.length; i++) {
        if ((i + 1) < lat_lng.length) {
          var src = lat_lng[i];
          var des = lat_lng[i + 1];
          // path.push(src);
          poly.setPath(path);
          service.route({
            origin: src,
            destination: des,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
          }, function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
              for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
                path.push(result.routes[0].overview_path[i]);
              }
            }
          });
        }
      }
    }
    html,
    body,
    #dvMap {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="http://maps.google.com/maps/api/js"></script>
    <div id="dvMap" ></div>

    0 讨论(0)
提交回复
热议问题