Bing:directionsManger.getAllWaypoints() 不返回纬度/经度

Posted

技术标签:

【中文标题】Bing:directionsManger.getAllWaypoints() 不返回纬度/经度【英文标题】:Bing: directionsManger.getAllWaypoints() doesn't return latitude/longitude 【发布时间】:2015-01-08 18:53:54 【问题描述】:

我正在使用 Bing 地图 ajax 控件。在我的代码中,我有以下内容 -

function getMap()
      
 map = new Microsoft.Maps.Map(document.getElementById('mnMap'), 
 credentials: 'MyKey', 
 mapTypeId: Microsoft.Maps.MapTypeId.road
          );
          map.setView(
 zoom: 4, 
 center: new Microsoft.Maps.Location(defaultLat, defaultLan)
          );
          Microsoft.Maps.loadModule('Microsoft.Maps.Directions', 
callback: createDirectionsManager
          );

      

      function createDirectionsManager()
      
          if (!directionsManager) 
          
              directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
          
          directionsManager.resetDirections();
          directionsManager.setRenderOptions( itineraryContainer: document.getElementById('directionsPanel') );
          directionsErrorEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', displayRouteError );
          directionsUpdatedEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', displayUpdatedRoute );
      

      function displayUpdatedRoute(status)

   // update waypoint text inputs based on dragged markers
          var legs = directionsManager.getAllWaypoints();
// Do some validation
      

 function displayRouteError(error)

         // If the error is a viapoint error, display an error
         if (error.responseCode == Microsoft.Maps.Directions.RouteResponseCode.noSolution)
 directionsManager.resetDirections();
         else if (error.responseCode == Microsoft.Maps.Directions.RouteResponseCode.dataSourceNotFound || error.responseCode == Microsoft.Maps.Directions.RouteResponseCode.tooFar)
directionsManager.resetDirections();
         else
directionsManager.resetDirections();
         
      
function getDirections(submit, send) 

directionsManager.resetDirections();
if (some test condition) 
          start = new Microsoft.Maps.Directions.Waypoint( location: new Microsoft.Maps.Location(locInputs.first().attr("data-lat"), locInputs.first().attr("data-lng")) );
         else 
          start = new Microsoft.Maps.Directions.Waypoint( address: locInputs.first().val() );
        
directionsManager.addWaypoint(start); // waypoint values come from UI based on user input string address
directionsManager.addWaypoint(waypoint);
directionsManager.addWaypoint(end);

directionsManager.setRenderOptions( itineraryContainer: document.getElementById('directionsPanel') );
        directionsManager.calculateDirections();

function saveTrip()

 var legs = directionsManager.getAllWaypoints();

 // do some validations
 //ajax call to backend


$("#saveTripBtn").click(function() 
getDirections();
                saveTrip();

        

getMap() 已正确初始化,我可以看到正确显示的方向。在方向管理器返回的航点中。在调用后端代码之前,我需要的是 savetrip() 方法中每个航点的纬度/经度,我没有看到。

我现在使用的是开发者密钥。让我知道是否需要提供更多信息。

提前致谢

肉山

【问题讨论】:

只是想添加-如果我通过地址查询 SearchManager 类-我确实得到了纬度/经度 【参考方案1】:

使用 Waypoint 对象的 getLocation 方法获取其位置坐标:http://msdn.microsoft.com/en-us/library/hh312838.aspx

这是一个工作代码示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>

      <script type="text/javascript">
        var map, directionsManager;

        function GetMap()  
           map = new Microsoft.Maps.Map(document.getElementById('myMap'),  
               credentials: 'YOUR_BING_MAPS_KEY'
           ); 

           Microsoft.Maps.loadModule('Microsoft.Maps.Directions', 
            callback: getDirections
           );
       

       function getDirections() 
            if (!directionsManager) 
            
               directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
            

            directionsManager.resetDirections();

            var start = new Microsoft.Maps.Directions.Waypoint( address: "Seattle, WA" );
            var end = new Microsoft.Maps.Directions.Waypoint( address: "Portland, OR" );

            directionsManager.addWaypoint(start); 
            directionsManager.addWaypoint(end);
            directionsManager.calculateDirections(); 
        

        function getWaypoints()
            var wp = directionsManager.getAllWaypoints();

            var text = '';

            for(var i=0;i<wp.length;i++)
                var loc = wp[i].getLocation();
                text += 'waypoint ' + i + ': ' + loc.latitude + ', ' + loc.longitude + '\r\n';
            

            document.getElementById('output').innerText = text;
        
      </script>
   </head>
   <body onload="GetMap();">
    <div id='myMap' style=";width:600px;height:400px;"></div><br/>
    <input type="button" onclick="getWaypoints()" value="Get Waypoints" /><br/>
    <div id="output"></div>
   </body>
</html>

【讨论】:

我怀疑你使用不正确。看一下代码示例。这行得通。 谢谢瑞奇。错误是 - 我在以下事件完成之前调用了方向管理器.getAllWaypoints()。结果从未收到位置数据。 DirectionsUpdatedEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', displayUpdatedRoute);

以上是关于Bing:directionsManger.getAllWaypoints() 不返回纬度/经度的主要内容,如果未能解决你的问题,请参考以下文章

获取Bing每日图片API接口

总是跳转到国内版(cn.bing.com)?New Bing使用全攻略

win8的bing桌面(9m左右)怎么实现自动更新bing桌面壁纸,壁纸都保存到哪了。求大神解答

搜索引擎Bing更名,强化微软品牌

Bing.com网站

Bing.com网站