简体   繁体   English

Google Maps API在Ajax页面加载中显示灰色地图

[英]Google Maps API showing grey map on ajax page load

I'm using ajax to load (into a div) the page that includes the Google map, however when it's finished loading it shows only a grey background for the map, with a Google logo in the bottom left, and a "Terms of use" link in the bottom right. 我正在使用Ajax加载(包括div在内)包含Google地图的页面,但是加载完成后,它仅显示该地图的灰色背景,并在左下方显示Google徽标,并显示“使用条款” ”)。

If I resize the window, then the map instantly appears . 如果我调整窗口大小,则地图会立即出现 Also if I reload the entire page by pressing F5 the map loads normally. 另外,如果我按F5重新加载整个页面,则地图将正常加载。 It's only when I initially load this map page using jQuery ajax that I get the grey map. 仅当我最初使用jQuery ajax加载此地图页面时,我才获得灰色地图。

On the container page I load the maps api. 在容器页面上,我加载了maps api。

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>

On the map page I do everything else. 在地图页面上,我会做其他所有事情。

$(document).ready(function() {
  initializeMap();
});

function initializeMap() {
  var companyname = document.getElementById("companyname").value;

  infowindow = new google.maps.InfoWindow();
  latlng = new google.maps.LatLng(-34.397, 150.644);

  var mapOptions = {
    zoom: 13,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  geo = new google.maps.Geocoder();
  map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
  bounds = new google.maps.LatLngBounds();

  resultsPanel = new google.maps.DirectionsRenderer({suppressMarkers:true});
  resultsPanel.setMap(map);

  $("#progressbar").progressbar(0);

  var companyaddress = getCompanyAddress();
  setCompanyMarker(companyaddress,companyname)
}

On resize I recalculate the height so that it fills the full page. 调整大小后,我会重新计算高度,以便填满整个页面。 If I don't do this then the map opens with 1px height, though I don't know why. 如果我不这样做,那么地图将以1px的高度打开,尽管我不知道为什么。 Could that be the same issue? 可能是同一问题吗?

function calculateHeight(){
  var height = $('#maincontent').height();
  $('#map-canvas').css("height",height);
}

$(window).resize(function () {
  calculateHeight();
});

Since the map appears when the user resizes the window manually I thought programatically calling the resize event would have the same effect, so I added this: 由于地图是在用户手动调整窗口大小时出现的,所以我认为以编程方式调用resize事件将具有相同的效果,因此我添加了以下内容:

$(window).trigger('resize');

But that did nothing. 但这没什么。

I tried moving the api script to the map page but that caused a " You've loaded the same script multiple times " error if I load the page more than once, which then triggered other errors. 我尝试将api脚本移动到地图页面,但是如果我多次加载该页面,则会导致“ 多次加载相同的脚本 ”错误,然后触发其他错误。 However, with this setup I was able to load the map successfully using ajax after three page loads . 但是,通过这种设置,我能够在三页加载之后使用ajax成功加载地图。 In other words, after loading the container page once, I have to click on the menu item (that loads the map page) three times to get it to load properly. 换句话说,在加载一次容器页面之后,我必须单击菜单项(加载地图页面)三次,以使其正确加载。 the first two times it still fails to load properly, but after that it loads properly every time, though with a load of JavaScript errors. 前两次,它仍然无法正确加载,但是此后,尽管有JavaScript错误,但每次都可以正确加载。

I also tried loading the api script asynchronously as per this suggestion . 我还尝试按照此建议异步加载api脚本。

I also tried adding "async defer" to the api script as per this suggestion . 我还尝试按照此建议在api脚本中添加“异步延迟”。

Neither of these suggestions worked, and now I'm here. 这些建议都不起作用,现在我在这里。

What is causing the map to load with a grey background when the page is loaded via ajax, and what can I do about it? 通过ajax加载页面时,导致地图以灰色背景加载的原因是什么,我该怎么办?

1.Make sure map-canvas have width set in CSS. 1.确保map-canvas具有width在CSS设置。

2.When exactly did you try calling google.maps.event.trigger(map,'resize'); 2.您何时确切尝试致电google.maps.event.trigger(map,'resize'); as geocodezip suggested? 如geocodezip建议? You need to call it after any map-canvas div's dimension change ( calculateHeight ) or any change to visibility of maps div . 你不需要任何后打电话到其map-canvas div的尺寸变化( calculateHeight )或将地图的可见性的任何变化div If the map-canvas div, or it's parent, or it's parent's parent (any predecessor) has display:none set at some point, the map view won't initialise properly and you will only see gray map. 如果map-canvas格,或者它的父,或它的父的父(任何前身)具有display:none设定在某些时候,地图视图将无法正常初始化,你将只能看到灰色的地图。 Also add the map resize code into some timeout to be sure, and then lower it if it works, eg: 同时添加的地图大小调整代码到一些超时可以肯定的,然后放下它,如果它的工作原理,如:

function calculateHeight(){
  var height = $('#maincontent').height();
  $('#map-canvas').css("height",height);
  setTimeout(function(){ //also do this after any other visibility/dimension change
     google.maps.event.trigger(map,'resize');
  }, 500);
}

Try this, which worked for me google.maps.event.addListener(map, 'idle', function() { google.maps.event.trigger(map, 'resize'); }); map.setZoom( map.getZoom() - 1 ); map.setZoom( map.getZoom() + 1 ); 试试这个,对我google.maps.event.addListener(map, 'idle', function() { google.maps.event.trigger(map, 'resize'); }); map.setZoom( map.getZoom() - 1 ); map.setZoom( map.getZoom() + 1 ); google.maps.event.addListener(map, 'idle', function() { google.maps.event.trigger(map, 'resize'); }); map.setZoom( map.getZoom() - 1 ); map.setZoom( map.getZoom() + 1 );

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

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