简体   繁体   English

Google Maps Api显示灰色地图

[英]Google Maps Api shows grey map

I'm trying to integrate Google Maps Javascript API in my wordpress website. 我正在尝试在我的wordpress网站中集成Google Maps Javascript API。

I created an API key, and I added a pure html code block in my page. 我创建了一个API密钥,并在页面中添加了一个纯HTML代码块。

Before it worked perfectly, but now it only show a grey piece. 在它完美运作之前,但现在它只显示一个灰色的部分。 When I zoom out and drag the map around, the following picture is visible: 当我缩小并拖动地图时,可以看到以下图片:

在此输入图像描述

This is my code in the html block: 这是我在html块中的代码:

<div id="googlemaps"></div>
<script>
    var map;
    function initMap() {
        map = new google.maps.Map(document.getElementById('googlemaps'), {
            center: {lat: 51.341667, lng: 4.21444},
            zoom: 8
         });
    }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR-API-KEY&callback=initMap"
async defer></script>

And YES, I replaced YOUR-API-KEY with my API key in my code (this is just so others won't use the generated code). 是的,我在我的代码中用我的API密钥替换了YOUR-API-KEY(这是其他人不会使用生成的代码)。

I don't know what is wrong with the map. 我不知道地图有什么问题。 I do not have errors in my webconsole. 我的webconsole中没有错误。

Thanks in advance! 提前致谢!

 <script> var map; function initMap() { map = new google.maps.Map(document.getElementById('googlemaps'), { center: {lat: 51.341667, lng: 4.21444}, zoom: 8, backgroundColor: '#fffff', }); } </script> <script src="https://maps.googleapis.com/maps/api/js?key=YOUR-API-KEY&callback=initMap" async defer></script 
 <div id="googlemaps"></div> 

Use backgroundColor property 使用backgroundColor属性

Call google.maps.event.trigger(map, 'resize') inside initMap() as you have resized the map div. 在调整map div的大小时,在initMap()中调用google.maps.event.trigger(map,'resize')。

 var map;
function initMap() {
    map = new google.maps.Map(document.getElementById('googlemaps'), {
        center: {lat: 51.341667, lng: 4.21444},
        zoom: 8
     });
google.maps.event.trigger(map, 'resize')
}

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

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