2011-12-12 93 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. 現在繪製你的三個圓圈。

你的問題是時間和正確的順序之一。