Google Maps - scaling to the size of markers does not work when the map is not visible

I am using the Google Maps API v2. I add markers to the map and then scale to fit these markers. This works great if the map is visible, I do it. But if not - for example, if I have a tab, and the map tab will not be selected when the page loads - then when I show the map, the zoom level and center will be wrong.

Here is a simple test case (uses jQuery):

<script type="text/javascript">

    var scale = Math.random() * 20;

    $(document).ready(function() {
        var $container = $('#container');
        // $container.hide();
        var map = new GMap2($('#map')[0]);
        $container.show();
        var markerBounds = new GLatLngBounds();
        for (var i = 0; i < 10; i++) {
            var randomPoint = new GLatLng(38.935394 + (Math.random() - 0.5) * scale, -77.061382 + (Math.random() - 0.5) * scale);
            map.addOverlay(new GMarker(randomPoint));
            markerBounds.extend(randomPoint);
        }
        map.setCenter(markerBounds.getCenter(), map.getBoundsZoomLevel(markerBounds));
    });

</script>

<div id="container">
    <div id="map" style="margin: 100px; width: 450px; height: 300px;"></div>
</div>

This works fine, but if you uncomment $container.hide(), everyone deleted it.

Is there a way to get the Google Maps API to work fine on a div that is not displaying?

+3
2

, , GMaps2() . hide() , , getBoundsZoomLevel(), show(), .

:

$(document).ready(function() {
   var $container = $('#container');

   // First create the Map.
   var map = new GMap2($('#map')[0]);

   // The container can be hidden immediately afterwards.
   $container.hide();

   // Now you can do whatever you like!
   var markerBounds = new GLatLngBounds();
   for (var i = 0; i < 10; i++) {
      var randomPoint = new GLatLng( 38.935394 + (Math.random() - 0.5) * scale, 
                                    -77.061382 + (Math.random() - 0.5) * scale);
      map.addOverlay(new GMarker(randomPoint));
      markerBounds.extend(randomPoint);
   }
   map.setCenter(markerBounds.getCenter(), map.getBoundsZoomLevel(markerBounds));

   // Finally unhide the container.
   $container.show();
});

getBoundsZoomLevel

+1

, , .

            $(".TabPanel").watch("display,visibility", function() {
                $(".MapContainer", this).each(function() {
                    if ($(this).is(":visible") == true) {
                        $(this).zoomToFitMarkers();
                    };
                });
            });

Rick Strahl jQuery, , .

zoomToFitMarkers:

$.fn.zoomToFitMarkers = function() {
    var map = this[0];
    map.gmap.checkResize();
    map.bounds = new GLatLngBounds();
    if (!!map.gmap.getOverlays) {
        for (i = 0; i < map.gmap.getOverlays.length; i++) {
            map.bounds.extend(map.gmap.getOverlays[i].getLatLng());
        }
        if (map.bounds && !map.bounds.isEmpty()) {
            var zoomLevel = map.gmap.getBoundsZoomLevel(map.bounds);
            zoomLevel = zoomLevel > 9 ? 9 : zoomLevel;
            zoomLevel = zoomLevel < 2 ? 2 : zoomLevel;
            map.gmap.setCenter(map.bounds.getCenter(), zoomLevel);
        }
    }
    map.gmap.checkResize();
};

:

  • GMap2 map.gmap, map DOM:

    var map= $("div#MapTarget")[0];
    map.gmap = new google.maps.Map2(map);
    
  • , , :

    var marker = new GMarker(point);
    map.gmap.addOverlay(marker);
    // Keep track of new marker in getOverlays array
    if (!map.gmap.getOverlays) map.gmap.getOverlays = new Array();
    map.gmap.getOverlays.push(marker);
    
+1

Source: https://habr.com/ru/post/1734697/


All Articles