Google Maps v3 fitBounds() Zoom too close for single marker

前端 未结 17 2072
别那么骄傲
别那么骄傲 2020-12-12 23:28

Is there a way to set a max zoom level for fitBounds()? My problem is that when the map is only fed one location, it zooms in as far as it can go, which really

相关标签:
17条回答
  • 2020-12-12 23:52

    u can use

    map.setOptions({
        maxZoom: [what u want],
        minZoom: [what u want]
    });
    

    this way u set the properties of the map after the map has been initialized .... u can set them as many times as u want ... but in ur case ... u can set them before fitBounds()

    good luck, rarutu

    0 讨论(0)
  • 2020-12-12 23:54

    Another solution is to expand bounds if you detect they are too small before you execute fitBounds():

    var bounds = new google.maps.LatLngBounds();
    // here you extend your bound as you like
    // ...
    if (bounds.getNorthEast().equals(bounds.getSouthWest())) {
       var extendPoint = new google.maps.LatLng(bounds.getNorthEast().lat() + 0.01, bounds.getNorthEast().lng() + 0.01);
       bounds.extend(extendPoint);
    }
    map.fitBounds(bounds);
    
    0 讨论(0)
  • 2020-12-12 23:54

    This gives you a direct control upon max allowed zoom on bounds fitting.

    var fitToMarkers = function(map, markers, maxZoom) {
        if (typeof maxZoom == 'undefined') maxZoom = 15;
    
        google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
            if (this.getZoom() > maxZoom) {
                this.setZoom(maxZoom);
            }
        });
    
        var bounds = new google.maps.LatLngBounds();
        for (var m = 0; m < markers.length; m++) {
            var marker = markers[m];
            var latlng = marker.getPosition();
            bounds.extend(latlng);
        }
    
        map.fitBounds(bounds);
    };
    
    0 讨论(0)
  • 2020-12-12 23:56

    I have soulution based on limiting max zoom when fitting bounds. Works for me (tested on Win 7 - IE 9, FF 13, Chrome 19):

    // When fitting bounds:
    var bounds = new google.maps.LatLngBounds();
    // ...
    // extend bounds as you like
    // ..
    
    // now set global variable when fitting bounds
    window.fittingBounds = true;
    map.fitBounds(bounds);
    window.fittingBounds = false;
    
    
    // attach this event listener after map init
    google.maps.event.addListener(map, 'zoom_changed', function() {
        // set max zoom only when fitting bounds
        if (window.fittingBounds && map.getZoom() > 16) {
            this.setZoom(16);
        }
    });
    
    0 讨论(0)
  • 2020-12-12 23:59

    The way I prevent the map from zooming in to far is by adding this line of code:

    var zoomOverride = map.getZoom();
            if(zoomOverride > 15) {
            zoomOverride = 15;
            }
          map.setZoom(zoomOverride);
    

    Directly after this line:

    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    

    Feel free to change the zoom level to whatever level you don’t want the map to zoom past.

    If you have any problems or questions, just leave me a comment on the blog post I wrote about this at http://icode4you.net/creating-your-own-store-locator-map-how-to-prevent-the-map-from-zooming-in-too-close-on-a-single-marker

    0 讨论(0)
  • 2020-12-12 23:59

    As for me guys i solve it by creating an idle event after fitBounds. Working perfectly. Guess that's one of the most clean solutions here

    var locations = [['loc', lat, lng], ['loc', lat, lng]];
    .....
    for (i = 0; i < locations.length; i++) { 
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10
      });
      .... create markers, etc.
    }
    ....
    map.fitBounds(bounds);  
    google.maps.event.addListenerOnce(map, 'idle', function() {
      if (locations.length == 1) {
        map.setZoom(11);
      }
    });
    
    0 讨论(0)
提交回复
热议问题