Google Maps API v3: Can I setZoom after fitBounds?

前端 未结 23 2324
执笔经年
执笔经年 2020-11-27 09:35

I have a set of points I want to plot on an embedded Google Map (API v3). I\'d like the bounds to accommodate all points unless the zoom level is too low (i.e., zoomed out

相关标签:
23条回答
  • 2020-11-27 09:42

    For me the easiest solution was this:

    map.fitBounds(bounds);
    
    function set_zoom() {
        if(map.getZoom()) {map.setZoom(map.getZoom() - 1);}
        else {setTimeout(set_zoom, 5);}
    }
    setTimeout(set_zoom, 5);
    
    0 讨论(0)
  • 2020-11-27 09:44

    I've found a solution that does the check before calling fitBounds so you don't zoom in and suddenly zoom out

    var bounds = new google.maps.LatLngBounds();
    
    // extend bounds with each point
    
    var minLatSpan = 0.001;
    if (bounds.toSpan().lat() > minLatSpan) {
        gmap.fitBounds(bounds); 
    } else {
        gmap.setCenter(bounds.getCenter());
        gmap.setZoom(16);
    }
    

    You'll have to play around with the minLatSpan variable a bit to get it where you want. It will vary based on both zoom-level and the dimensions of the map canvas.

    You could also use longitude instead of latitude

    0 讨论(0)
  • 2020-11-27 09:48

    I use this to ensure the zoom level does not exceed a set level so that I know satellite images will be available.

    Add a listener to the zoom_changed event. This has the added benefit of controlling the zoom control on the UI also.

    Only execute setZoom if you need to, so an if statement is preferable to Math.max or to Math.min

       google.maps.event.addListener(map, 'zoom_changed', function() { 
          if ( map.getZoom() > 19 ) { 
            map.setZoom(19); 
          } 
        });
        bounds = new google.maps.LatLngBounds( ... your bounds ... )
        map.fitBounds(bounds);
    

    To prevent zooming out too far:

       google.maps.event.addListener(map, 'zoom_changed', function() { 
          if ( map.getZoom() < 6 ) { 
            map.setZoom(6); 
          } 
        });
        bounds = new google.maps.LatLngBounds( ... your bounds ... )
        map.fitBounds(bounds);
    
    0 讨论(0)
  • 2020-11-27 09:48

    In this function, you need to dynamically add metadata to store the geometry type only because the function accepts any geometry.

    "fitGeometries" is a JSON function extending a map object.

    "geometries" is an generic javascript array not an MVCArray().

    geometry.metadata = { type: "point" };
    var geometries = [geometry];
    
    fitGeometries: function (geometries) {
        // go and determine the latLngBounds...
        var bounds = new google.maps.LatLngBounds();
        for (var i = 0; i < geometries.length; i++) {
            var geometry = geometries[i];
            switch (geometry.metadata.type)
            {
                case "point":
                    var point = geometry.getPosition();
                    bounds.extend(point);
                    break;
                case "polyline":
                case "polygon": // Will only get first path
                    var path = geometry.getPath();
                    for (var j = 0; j < path.getLength(); j++) {
                        var point = path.getAt(j);
                        bounds.extend(point);
                    }
                    break;
            }
        }
        this.getMap().fitBounds(bounds);
    },
    
    0 讨论(0)
  • 2020-11-27 09:48

    All I did is:

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

    And it works on V3 API.

    0 讨论(0)
  • 2020-11-27 09:50

    Please try this.

    // Find out what the map's zoom level is
    zoom = map.getZoom();
    if (zoom == 1) {
      // If the zoom level is that low, means it's looking around the
    world.
      // Swap the sw and ne coords
      viewportBounds = new
    google.maps.LatLngBounds(results[0].geometry.location, initialLatLng);
      map.fitBounds(viewportBounds);
    }
    

    If this will helpful to you.

    All the best

    0 讨论(0)
提交回复
热议问题