Restrict the min/max zoom on a Bing Map with v7 of the AJAX control?

前端 未结 3 1838
谎友^
谎友^ 2021-01-02 09:07

I\'m working on a site that makes use of v7 of the Bing Maps AJAX Control. One of the things I need to do is restrict the zoom level so as to prevent users from zoom in past

3条回答
  •  一生所求
    2021-01-02 09:32

    According to Bing Maps support, the only way to do this (which isn't particularly elegant, and results in some unwelcome jitter on the map) is as follows:

    // "map" is our Bing Maps object, overload the built-in getZoomRange function
    // to set our own min/max zoom
    map.getZoomRange = function ()
    {
      return {
        max:      14,
        min:      5
      };
    };
    
    // Attach a handler to the event that gets fired whenever the map's view is about to change
    Microsoft.Maps.Events.addHandler(map,'viewchangestart',restrictZoom);
    
    // Forcibly set the zoom to our min/max whenever the view starts to change beyond them 
    var restrictZoom = function ()
    {
      if (map.getZoom() <= map.getZoomRange().min) 
      {
        map.setView({
          'zoom':       map.getZoomRange().min,
          'animate':    false
        });
      }
      else if (map.getZoom() >= map.getZoomRange().max) 
      {
        map.setView({
          'zoom':       map.getZoomRange().max,
          'animate':    false
        });
      }
    };
    

提交回复
热议问题