2011-12-12 97 views
0

我目前正在开发一个使用Bing地图的网站。我使用Bing Maps版本7.我已经创建了一个全功能的驾车路线指引功能。它的作用就像这样:Bing地图中行车路线的圆圈半径

用户在地图上点击右键,无所谓。然后出现一个上下文菜单,用户可以在两个选择之间进行选择。它是:Allign开始Allign完成

正如你可能知道这些函数正在用户右键点的位置上创建一个航点。在相应的航点上也分配一个半径圆。起点和终点的航点均为可拖动/可移动,这意味着用户可以移动路标。问题是,当用户移动其中一个路径点时,半径圆也不会移动,这并不奇怪,因为我还没有创建一个函数。但我不认为这有点难,但我不知道如何获得移动的航点的新位置。我张贴我的代码。所以我真的需要一些帮助,使这个“RadiusCircleMove”。提前致谢。

这是我的JavaScript代码:

var map = null; 
var directionsManager; 
var directionsErrorEventObj; 
var directionsUpdatedEventObj; 
var startPosition; 
var checkpointPosition; 
var finishPosition; 
var popuplat; 
var popuplon; 
var waypointType; 
var startcircle; 
var checkpointcircle; 
var finishcircle; 
var startcirclelat; 
var startcirclelon; 
var checkpointcirclelat; 
var checkpointcirclelon; 
var finishcirclelat; 
var finishcirclelon; 

$(document).ready(function() { 
    //this one line will disable the right mouse click menu 
    $(document)[0].oncontextmenu = function() { return false; } 
    GetMap(); 
}); 

function GetMap() { 
    map = new Microsoft.Maps.Map(document.getElementById("myMap"), { credentials: "Enter Bing Key Here", zoom: 4, center: new Microsoft.Maps.Location(45, -100) }); 
    Microsoft.Maps.registerModule("BMv7.AdvancedShapes", "BMv7.AdvancedShapes.min.js"); 
    Microsoft.Maps.loadModule("BMv7.AdvancedShapes"); 
    //map.AttachEvent("onclick", ShowPopupMenu); 
    Microsoft.Maps.Events.addHandler(map, 'click', RemovePopupMenu); 
    Microsoft.Maps.Events.addHandler(map, 'rightclick', ShowPopupMenu); 
} 

function ShowPopupMenu(e) { 
    var point = new Microsoft.Maps.Point(e.getX(), e.getY()); 
    popuplat = e.target.tryPixelToLocation(point).latitude 
    popuplon = e.target.tryPixelToLocation(point).longitude 
    var menu = document.getElementById('popupmenu'); 
    menu.style.display = 'block'; //Showing the menu 
    menu.style.left = e.pageX + "px"; //Positioning the menu 
    menu.style.top = e.pageY + "px"; 
} 

function RemovePopupMenu() { 
    document.getElementById("popupmenu").style.display = 'none'; 
} 

function createDirectionsManager() { 
    var displayMessage; 
    if (!directionsManager) { 
     directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map); 
     //displayMessage = 'Directions Module loaded\n'; 
     //displayMessage += 'Directions Manager loaded'; 
    } 
    //alert(displayMessage); 
    directionsManager.resetDirections(); 
    directionsUpdatedEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', function() {}); 
} 

function createDrivingRoute() { 

     if (!directionsManager) { createDirectionsManager(); } 

     directionsManager.resetDirections(); 

     // Set Route Mode to driving 

      directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving }); 

      if (waypointType == "start") { 
       addDefaultPushpin(); 
       startPosition = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(popuplat, popuplon) }); 
       startcirclelat = popuplat; 
       startcirclelon = popuplon; 
      } 

      if (waypointType == "checkpoint") { 
       addDefaultPushpin(); 
       checkpointPosition = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(popuplat, popuplon) }); 
       checkpointcirclelat = popuplat; 
       checkpointcirclelon = popuplon; 
      } 

      if (waypointType == "finish") { 
       finishPosition = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(popuplat, popuplon) }); 
       finishcirclelat = popuplat; 
       finishcirclelon = popuplon; 
       directionsManager.addWaypoint(startPosition); 
       directionsManager.addWaypoint(checkpointPosition); 
       directionsManager.addWaypoint(finishPosition); 
       directionsManager.calculateDirections(); 
       deletePushpin(); 
       CreateStartCircle(); 
       CreateCheckpointCircle(); 
       CreateFinishCircle(); 
      } 
      // Set the element in which the itinerary will be rendered 

      directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsItinerary') }); 

      //alert('Calculating directions...'); 


} 

function createDirections() { 
    if (!directionsManager) { 
     Microsoft.Maps.loadModule('Microsoft.Maps.Directions', { callback: createDrivingRoute }); 
    } 
    else { 
     createDrivingRoute(); 
    } 
} 

function AddStartPosition() { 
    waypointType = "start"; 
    createDirections(); 
    RemovePopupMenu(); 
} 

function AddCheckpointPosition() { 
    waypointType = "checkpoint"; 
    createDirections(); 
    RemovePopupMenu(); 
} 

function AddFinishPosition() { 
    waypointType = "finish"; 
    createDirections(); 
    RemovePopupMenu(); 
} 
    function addDefaultPushpin() { 
     var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(popuplat, popuplon)); 
     map.entities.push(pushpin); 
    } 

    function deletePushpin() { 
     for (var i = map.entities.getLength() - 1; i >= 0; i--) { 
      var pushpin = map.entities.get(i); 
      if (pushpin instanceof Microsoft.Maps.Pushpin) { 
       map.entities.removeAt(i); 
      }; 
     } 
    } 
    function CreateStartCircle() { 
     startcircle = DecStartCircle(); 
     map.entities.push(startcircle); 
    } 
    function CreateCheckpointCircle() { 
     checkpointcircle = DecCheckpointCircle(); 
     map.entities.push(checkpointcircle); 
    } 
    function CreateFinishCircle() { 
     finishcircle = DecFinishCircle(); 
     map.entities.push(finishcircle); 
    } 

     /***** Start Circle ****/ 
    function DecStartCircle() { 
     var polygonOptions = { 
      fillColor: new Microsoft.Maps.Color(100, 0, 0, 255), 
      strokeColor: new Microsoft.Maps.Color(100, 255, 0, 0) 
     }; 
     return new BMv7.AdvanceShapes.Circle(new Microsoft.Maps.Location(startcirclelat, startcirclelon), 80000, polygonOptions); 
    } 

    /***** Checkpoint Circle ****/ 
    function DecCheckpointCircle() { 
     var polygonOptions = { 
      fillColor: new Microsoft.Maps.Color(100, 0, 0, 255), 
      strokeColor: new Microsoft.Maps.Color(100, 255, 0, 0) 
     }; 
     return new BMv7.AdvanceShapes.Circle(new Microsoft.Maps.Location(checkpointcirclelat, checkpointcirclelon), 80000, polygonOptions); 
    } 

    /***** Finish Circle ****/ 
    function DecFinishCircle() { 
     var polygonOptions = { 
      fillColor: new Microsoft.Maps.Color(100, 0, 0, 255), 
      strokeColor: new Microsoft.Maps.Color(100, 255, 0, 0) 
     }; 
     return new BMv7.AdvanceShapes.Circle(new Microsoft.Maps.Location(finishcirclelat, finishcirclelon), 80000, polygonOptions); 
    } 
+0

'$(document)[0] .oncontextmenu = function(){return false; }''可能有资格成为史上最笨的代码行。请使用'document.oncontextmenu'或'$(document).on(“contextmenu”)'。 –

回答

0

我相信你需要真正实现您DirectionsUpdated事件。您的代码包含以下空白功能:

directionsUpdatedEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', function() {}); 

在导航完成加载之前,您不应该对导图做任何操作。一旦他们已加载,那么你就可以做到以下几点:

  1. 清除其删除所有以前的折线和多边形所有地图实体map.entities.clear();
  2. 然后复位方向:directionsManager.resetDirections();清除电流方向(否则你会看到航点所附)。
  3. 然后get all of the waypoints从方向模块directionsManager.getAllWaypoints();
  4. 现在绘制你的三个圆圈。

你的问题是时间和正确的顺序之一。