简体   繁体   English

谷歌地图响应式调整大小

[英]Google maps responsive resize

I'm trying to get google maps responsive and resize while keeping its center when windows resizes.我试图让谷歌地图响应并调整大小,同时在窗口调整大小时保持其中心。 I read other stack questions in regards such as:我阅读了其他堆栈问题,例如:

Responsive Google Map? 响应式谷歌地图? and Center Google Maps (V3) on browser resize (responsive)在浏览器调整大小(响应式)上居中 Google Maps (V3)

from the second stack question I got a link which helps me with part of the code but I still have no luck.从第二个堆栈问题我得到了一个链接,它可以帮助我处理部分代码,但我仍然没有运气。 This is the code I am using, when I resize the window, the maps doesn't resize at all这是我正在使用的代码,当我调整窗口大小时,地图根本不会调整大小

    function initialize() {
      var mapOptions = {
       center: new google.maps.LatLng(40.5472,12.282715),
       zoom: 6,
       mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map-canvas"),
                mapOptions);
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    google.maps.event.addDomListener(window, "resize", function() {
     var center = map.getCenter();
     google.maps.event.trigger(map, "resize");
     map.setCenter(center); 
    });

html html

 <div id="map-canvas"/>

css css

html { height: 100% }
body { height: 100%; margin: 0; padding: 0; }
#map-canvas { height: 100%; }

Move your map variable into a scope where the event listener can use it.将您的地图变量移动到事件侦听器可以使用它的范围内。 You are creating the map inside your initialize() function and nothing else can use it when created that way.您正在 initialize() 函数中创建地图,并且以这种方式创建时没有其他任何东西可以使用它。

var map; //<-- This is now available to both event listeners and the initialize() function
function initialize() {
  var mapOptions = {
   center: new google.maps.LatLng(40.5472,12.282715),
   zoom: 6,
   mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addDomListener(window, "resize", function() {
 var center = map.getCenter();
 google.maps.event.trigger(map, "resize");
 map.setCenter(center); 
});

After few years, I moved to leaflet map and I have fixed this issue completely, the following could be applied to google maps too:几年后,我转向了传单地图,我已经完全解决了这个问题,以下内容也适用于谷歌地图:

    var headerHeight = $("#navMap").outerHeight();
    var footerHeight = $("footer").outerHeight();
    var windowHeight = window.innerHeight;
    var mapContainerHeight = headerHeight + footerHeight;
    var totalMapHeight = windowHeight - mapContainerHeight;
    $("#map").css("margin-top", headerHeight);
    $("#map").height(totalMapHeight);

    $(window).resize(function(){
        var headerHeight = $("#navMap").outerHeight();
        var footerHeight = $("footer").outerHeight();
        var windowHeight = window.innerHeight;
        var mapContainerHeight = headerHeight + footerHeight;
        var totalMapHeight = windowHeight - mapContainerHeight;
        $("#map").css("margin-top", headerHeight);
        $("#map").height(totalMapHeight);
        map.fitBounds(group1.getBounds());
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM