必应地图距离和路线图问题

时间:2018-07-18 04:47:00

标签: routes distance bing-maps bing

嗨,我正在尝试在bing地图上绘制25个位置并计算总距离。我正在arr [i]

在该循环中,i-1为开始,i为结束,在循环中的第一次迭代中,

为开始,i + 1为结束。

绘图时

每次刷新时都会给出不同的距离

错误代码-1

消息-我们在这些地方之间找不到任何路线。但是当我只绘制纬度和

经度。我可以看到路线。

也是情节

为什么会出现随机性?所采用的经度和纬度属于同一个城市,所以

理想地寻找路线应该不是问题。

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script type='text/javascript'
        src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap'>     </script>

<script type='text/javascript'>
    var map;
    var directionsManager;
    //kora,bellan,indira,tumkur,jpnagar

    var arrayoflat=[12.927,12.922,12.923,12.924,12.925,12.926,12.971,13.339,12.910,12.925,12.976,12.959,13.028,13.035,13.042,13.042,12.908,12.898,12.916,12.839,12.816,12.854,12.991,12.972,13.025,12.457];

    //,12.870,12.992,12.990,12.939,13.008];

    //,12.975,12.990,13.019,12.961,13.019,12.897,12.897,13.085,12.878,12.988,13.033,13.019,12.985,13.069,12.948,12.964,12.959,12.975,12.780,13.003,13.056,12.981,13.062,12.938,12.891,12.979,12.997,12.963,13.003];


    var arrayoflng=[77.627,77.628,77.630,77.640,77.645,77.676,77.641,77.1140,77.585,77.593,77.571,77.697,77.540,77.597,77.613,77.619,77.647,77.617,77.610,77.677,77.698,77.788,77.687,77.695,77.659,75.357];

    //,77.655,77.715,77.760,77.695,77.673];

    //,77.675,77.552,77.646,77.638,77.596,77.607,77.607,77.556,77.637,77.538,77.564,77.703,77.735,77.582,77.597,77.497,77.717,77.665,77.771,77.564,77.623,77.628,77.587,77.630,77.640,77.763,77.614,77.603,77.622];        



    console.log(arrayoflat.length);
    var distancefinal=0;
    var timefinal=0;

    function GetMap()
    {
        map = new Microsoft.Maps.Map('#myMap', {
            credentials: '',
            center: new Microsoft.Maps.Location(12.9715987, 77.5945627),
            zoom: 2
        });

        //Load the directions module.
        Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function ()
        {
            var a=arrayoflat.length;
            for(i=0;i<24;i++)
            {
                if(arrayoflat[i]!=undefined && arrayoflng[i]!=undefined)
                {
                    directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
                if(i==0)
                {


                       //getRoute(arrayoflat[i],arrayoflng[i], arrayoflat[i+1],arrayoflng[i+1], 'green');
                        var seattleWaypoint = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(arrayoflat[i], arrayoflng[i]) });
                        directionsManager.addWaypoint(seattleWaypoint);
                        var workWaypoint = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(arrayoflat[i+1],arrayoflng[i+1]) });
                        directionsManager.addWaypoint(workWaypoint);

                }
                else
                {
                    // getRoute(arrayoflat[i-1],arrayoflng[i-1],arrayoflat[i],arrayoflng[i],'red');
                    var seattleWaypoint = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(arrayoflat[i-1], arrayoflng[i-1]) });
                    directionsManager.addWaypoint(seattleWaypoint);
                    var workWaypoint = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(arrayoflat[i],arrayoflng[i]) });
                    directionsManager.addWaypoint(workWaypoint);

                }
                 directionsManager.setRenderOptions
                 ({
                    drivingPolylineOptions: 
                    {
                        strokeColor: 'red',
                        strokeThickness: 2
                    },
                    waypointPushpinOptions: 
                    {
                         text :'P'+ i
                    }
                });
               directionsManager.calculateDirections();
               Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', directionsError);
               Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', directionsUpdated);

            }   
        }

        });
    }

    function directionsUpdated(e) 
    {
        //console.log("kk");
        var routeIdx = 0;
        //directionsManager.getRequestOptions().routeIndex;
        var distance = Math.round(e.routeSummary[routeIdx].distance * 100)/100;
        var units = directionsManager.getRequestOptions().distanceUnit;
        var distanceUnits = '';
        if (units == Microsoft.Maps.Directions.DistanceUnit.km) 
        {
            distanceUnits = 'km'
        } else 
        {
            //Must be in miles
            distanceUnits = 'miles'
        }
        var time = Math.round(e.routeSummary[routeIdx].timeWithTraffic / 60);
        timefinal=timefinal+time;


        document.getElementById('routeInfoPanel').innerHTML = 'Distance: ' + distancefinal + ' ' + distanceUnits + '<br/>Time with Traffic: ' + timefinal + ' minutes';
    }

    function directionsError(e) 
    {
        alert('Error: ' + e.message + '\r\nResponse Code: ' +      e.responseCode + "pin no" +i)
    }
</script>
</head>
<body>
<div id="myMap" style="position:relative;width:800px;height:600px;"></div>
<div id='routeInfoPanel'></div>
</body>
</html>

0 个答案:

没有答案