简体   繁体   English

Google Maps无法呈现

[英]Google Maps Failing to render

Note: I thought it would be better to make a new question on this. 注意:我认为最好对此提出一个新的问题。

So I recently asked a question about why Google maps is not rendering properly . 所以我最近问了一个问题, 为什么Google地图不能正确渲染 Now the answer would seem straight forward and simple, accept my code looks like this: 现在答案似乎简单明了,接受我的代码如下:

  var map;
  function initialize() {
    var mapOptions = {
         zoom: 8,
         center: new google.maps.LatLng(-34.397, 150.644),
         mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    google.maps.event.trigger(map, "resize");
  }

   google.maps.event.addDomListener(window, 'load', initialize);

The issue is the map is still broken: 问题是地图仍然损坏:

在此处输入图片说明

This map is stored in a <div id="Map"></div> which has a height of 350. This Div that holds the map is part of Jquery-UI Tabs, so it also has jquery skinning attached to it which may affect things like size and so on. 此地图存储在高度为350的<div id="Map"></div>中。该存放地图的Div是Jquery-UI Tabs的一部分,因此它还附加了jquery外观。影响大小之类的东西。

With that said the map should just work. 这样说,地图应该可以正常工作了。

If I open the console and throw in: google.maps.event.trigger(map, "resize"); 如果我打开控制台并输入: google.maps.event.trigger(map, "resize"); the maps then works as expected. 地图将按预期工作。

I also had a Google Map (v3) embedded within a jQuery UI Tabs, and had to work around the issue with this fix: 我还在jQuery UI Tabs中嵌入了Google Map(v3),并且不得不通过以下解决方法解决此问题:

var initialized = false;

$('.tabs').find('.ui-tabs-nav li').each(function() {
    if($(this).find('a').text() === 'Location') {
        if($(this).hasClass('ui-state-active')) {
            initialize();
            initialized = true;
        } else {
            $(this).click(function() {
                if(!initialized) {
                    initialize();
                    initialized = true;
                }
            });
        }
    }
});

Note that initialize() should run your starting map code. 请注意initialize()应该运行您的起始地图代码。 There are lots of ways to slice-and-dice the initialization, but the point is that we don't do it until the tab we're looking for ("Location", in this case) is active. 有很多方法可以对初始化进行切片和切块,但要点是,只有在要查找的选项卡(在这种情况下为“ Location”)处于活动状态时,我们才进行此操作。

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

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