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

前端 未结 3 1878
谎友^
谎友^ 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:26

    I was dealing with a similar issue and I ended up doing something very similar to what MrJamin describes in his answer, with one (subtle, but major) difference: I added a handler for targetviewchanged. According to the official docs on MSDN, 'targetviewchanged' occurs when the view towards which the map is navigating changes. Also, instead of calling Map#getZoom, I used Map#getTargetZoom which returns the zoom level of the view to which the map is navigating. Note, this approach prevents jitter.

    Here's the shortened version of my code:

    function restrictZoom(map,min,max) {
      Microsoft.Maps.Events.addHandler(map,'targetviewchanged',function(){
        var targetZoom = map.getTargetZoom();
        var adjZoom = targetZoom;
    
        if(targetZoom > max) {
          adjZoom = max;
        } else if(targetZoom < min) {
          adjZoom = min;
        }
    
        if(targetZoom != adjZoom) {
          map.setView({zoom:adjZoom});
        }
      });
    }
    

提交回复
热议问题