Locking a Virtual Earth Map


There may be times when you don’t want to have a draggable map. Oh, yes all the flexibility of draggable maps makes AJAX and Virtual Earth so much fun! And, knowing you maybe be wreaking havoc on the image tile servers is so stimulating. Plus, with AJAX, Microsoft has to deal with all the throughput to the client and not your web servers – yay!


However, if you do want to lock a map you can do so in one of two ways.


1. If you know you want a locked map when you load a Virtual Earth map you can simply pass in “true” for the Boolean parameter of the map.LoadMap() method.


VEMap.LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch);


2. Sometimes, you may want a map to be draggable but then lock it to disallow a user from scrolling beyond a certain area. To do that, you’ll want to override the onMouseDown event. Overriding the onMouseDown event is easy. First, attach an event to override onMouseDown:


map.AttachEvent(“onmousedown”,DisableMap);


Then, you’ll want to include a function to basically do nothing when onMouseDown event is fired (instead of the default Virtual Earth actions):


function DisableMap()
{
 return true;
}


That’s it! Here’s the whole solution so you can just copy, paste, run.


<script>
      var map = null;
           
      function GetMap()
      {
         map = new VEMap(‘myMap’);
         map.LoadMap();
  
   // Attach an event handler for a mousedown event.
   // So when the mouse is down, DisableMap function gets called
   map.AttachEvent(“onmousedown”,DisableMap);
      }
 
   // returning true will disable the mousedown event therefore disabes dragging
   function DisableMap()
   {
        return true;
   }
  
</script>


CP

Comments (2)

  1. alexandercer says:

    thanks for your wonderful words…

    and, i have meet a problem that the map can not drag from 179.0e to 179.0w(longitude) smoothly (just when i want to cross the "international date line" from 179e to 179w,i have to drag back through Asia ,Europe and America to another side of the map).

    is there any possible to slove this question and let virtual earth map can drag across the "international data line" smoothly just like google map did.

    ps:i have to put some data to the sea in the map,but the area of the sea is just splited by the "international data line".so that i can not watch the result very clearly…

    please give me some advise or suggestion….