简体   繁体   English

Google地图为灰色

[英]Google Maps is grey

I am using google maps in a mobile application using html and javascript. 我在使用html和javascript的移动应用程序中使用google maps。 When the I load the map I am only able to see 5% of the map in the upper left corner. 当我加载地图时,我只能在左上角看到5%的地图。 95% of the div container is grey. div容器中有95%是灰色的。 When I want to check the div with Firebug the whole map is loaded suddenly. 当我想用Firebug检查div时,整个地图突然加载了。 What can that be? 那会是什么? I tried already several Stackoverflow threads but no solution worked for me. 我已经尝试了几个Stackoverflow线程,但是没有解决方案适合我。 Thank you. 谢谢。

Code: 码:

<link type="text/css" rel="stylesheet" href="jquery.mobile...>
<link type="text/css" rel="stylesheet" href="index.css">
<script type="text/javascript" src="jquery.mobile...></script>
<script type="text/javascript" src="jquery.mobile/jquery.mobile...></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js">       </script>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8" src="index.js"></script>

<div id="map" style="width: 700px; height: 700px;"></div>

var map = new google.maps.Map(document.getElementById('map'), {
       zoom: 10,
       center: new google.maps.LatLng(-33.92, 151.25),
       mapTypeId: google.maps.MapTypeId.ROADMAP                                
});

index.js index.js

$(document).ready(function() {
    $(window).resize(function() {
        google.maps.event.trigger(map, 'resize');
    });
});

When I use div attributes measured in % or em it doesnt work at all. 当我使用以%或em度量的div属性时,它根本不起作用。

Try calling the resize method on document ready too: 也尝试在文档上调用resize方法:

$(document).ready(function() {
    $(window).resize(function() {
        google.maps.event.trigger(map, 'resize');
    });
    google.maps.event.trigger(map, 'resize');
});

Check your mapOptions whether its center or other properties are correct. 检查mapOptions的中心或其他属性是否正确。 In my occasion, center property was uninterpretable by google map; 在我看来,Google地图无法解释中心属性; although the page was totally errorless in terms of css and jsp, it showed me just grey map all the time. 尽管该页面在css和jsp方面完全没有错误,但它始终向我显示灰色地图。

For my case, just calling map.refresh() fixed this issue. 就我而言,只需调用map.refresh()解决此问题。 I am using Gmaps. 我正在使用Gmaps。

This happened to me because the mapTypeId was NULL. 这发生在我身上,因为mapTypeId为NULL。

Try: 尝试:

map.setMapTypeId("roadmap");

In my case I had missed to provide the Longitude which came from an external API. 就我而言,我错过了提供来自外部API的经度。 Once the API was fixed to include Longitude, the issue was solved. 将API固定为包括经度后,问题就解决了。

I had a similar question using angular 5 我有一个类似的问题使用角度5
I could only get the map to refresh / recreate by using a setTimeout AND a setZoom (even though the level is the same) 我只能使用setTimeout setZoom来刷新/重新创建地图(即使级别相同)

// set timeout.
setTimeout(() => {
    console.log("attempting refresh");
    google.maps.event.trigger(this.map, "resize");
    this.map.setZoom(8);
},
100);

Working Plunker Here 在这里工作柱塞

What's fascinating about the refresh, is that it moves the map the second time it is displayed. 刷新的魅力在于,它在第二次显示地图时移动了地图。 Google maps is clearly not bug free . 谷歌地图显然不是没有错误的

In my case my map wasn't loading because I didn't set a zoom option. 在我的情况下,由于未设置zoom选项,因此未加载地图。

var map = new google.maps.Map(document.getElementById('map'), {
       zoom: 10, // Forgot to set this prop
       center: new google.maps.LatLng(-33.92, 151.25)                               
});

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

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