绘制了无法解释的额外折线 - Google Maps API v3

时间:2016-08-15 11:09:08

标签: javascript html google-maps google-maps-api-3

关于在地图上绘制的Polyline,我有一个奇怪的场景。在我发布代码之前,我将首先演示当我使用法线方向服务时会发生什么(两者都计算得到的结果相同= 总距离:62.734 km ):

enter image description here

现在,当我自己绘制方向时 - 这条直线出现在任何地方:

enter image description here

代码段:

<script type="text/javascript">
    var markers = [
            {
                "lat": '-26.2036247253418',
                "lng": '28.0086193084717'
            }
        ,
            {
                "lat": '-26.1259479522705',
                "lng": '27.9742794036865'
            }
        ,
            {
                "lat": '-25.8434619903564',
                "lng": '28.2100086212158'
            }
    ];
    window.onload = function () {
        var mapOptions = {
            center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
            zoom: 8,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var labelIndex = 0;
        var totalDistance = 0;

        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();
        //var image = 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png';
        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,
                label: labels[labelIndex++ % labels.length],
                //icon: image
            });
            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****************//

        //Initialize the Path Array
        var path = new google.maps.MVCArray();

        //Initialize the Direction Service
        var service = new google.maps.DirectionsService();

        var directionsDisplay = new google.maps.DirectionsRenderer({
          setMap: map
        });

        //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.strokeColor = '#'+Math.floor(Math.random()*16777215).toString(16);
                poly.setPath(path);
                service.route({
                    origin: src,
                    destination: des,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                }, function (result, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(result);
                        var myroute = directionsDisplay.directions.routes[0];
                        var distance = 0;

                        for (i = 0; i < myroute.legs.length; i++) {
                            distance += myroute.legs[i].distance.value;
                            //for each 'leg'(route between two waypoints) we get the distance and add it to the total
                        }

                        for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {                            
                            path.push(result.routes[0].overview_path[i]);
                            //console.log(result.routes[0].legs[0].distance);
                        }                        
                        totalDistance += distance;
                        document.getElementById('total').innerHTML = (totalDistance / 1000) + ' km';
                    }                    
                });
            }
        }        
    }
</script>
<div id="dvMap"></div>
<div><p>Total Distance: <span id="total"></span></p></div>

2 个答案:

答案 0 :(得分:1)

好的,所以要解决问题。只需删除以下行:

参考:Google Documentation - Simple Polylines

enter image description here

就这样,线路消失了:

enter image description here

答案 1 :(得分:0)

你是不是也在第一个和最后一个聚合物之间画了一条线? 你应该只在poly0和poly1,poly1和poly2等之间画线,但不能在poly100和poly0之间画线(如果poly100是最后一行)

这将解释从B点到A完成形状的直线。你不想完成形状,所以停止绘画。没有功能你可以设置不完成形状?

我只知道一个非常昂贵的工作,那就是沿着相同的路线从B到A的相反顺序追溯。但这可能不是你想要的