简体   繁体   English

Google Maps API(JavaScript):未显示地图

[英]Google Maps API (JavaScript): Maps not showing

I'm obviously making a thing which is meant to show a Map. 我显然正在制作要显示地图的东西。

Literally nothing happens - no Map, no Errors, no crash and burn. 从字面上看,什么都没有发生-没有地图,没有错误,没有崩溃和烧伤。

Here is my code: 这是我的代码:

<div style="height:100%; width: 100%;">
    <div id="map-canvas"></div>
</div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDpgrLi55xM68AaSJOQPX5nzEOV7sw4KVY&callback=initMap"></script>

<script>
    function initMap() {
        var mapCanvas = document.getElementById("map-canvas");
        var mapOptions = {
            center: new google.maps.LatLng(53.419824, -3.0509294),

            mapTypeId: 'satellite',
            scrollwheel: true
        }
        var map = new google.maps.Map(mapCanvas, mapOptions);
    }

</script>

I've tried changing the width/height of the parent div to PX's with no luck. 我尝试将父div的宽度/高度更改为PX,但是没有运气。

Any help is great 任何帮助都很棒

Cheers 干杯

一点影像帮助

For a better insight, view the full page code here: http://pastebin.com/XrRYgtjz 为了获得更好的见解,请在此处查看完整的页面代码: http : //pastebin.com/XrRYgtjz

I would say you have two issues. 我会说你有两个问题。

  1. your map div doesn't have a size (you don't specify the size of the containing element for the anonymous div <div style="height:100%; width: 100%;"> ). 您的地图div没有大小(您没有为匿名div <div style="height:100%; width: 100%;"> )指定包含元素的大小)。

  2. your MapOptions doesn't contain the required /manditory zoom property. 您的MapOptions不包含必需的 / manditory zoom属性。

proof of concept fiddle 概念证明

code snippet: 代码段:

 function initMap() { var mapCanvas = document.getElementById("map-canvas"); var mapOptions = { center: new google.maps.LatLng(53.419824, -3.0509294), zoom: 5, mapTypeId: 'satellite', scrollwheel: true } var map = new google.maps.Map(mapCanvas, mapOptions); } 
 html, body, #map-canvas { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <div style="height:100%; width: 100%;"> <div id="map-canvas"></div> </div> <script deferred async src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script> 

<div style="height:100%; width: 100%;">

Maybe problem in height . 也许是height问题。 Try change to 400px. 尝试更改为400px。 Or 要么

<div style="height:400px; width: 100%;">
    <div id="map-canvas" style="height:100%; width: 100%;></div>
</div>

EDIT 编辑

<div style="height:100%; width: 100%;">
    <div id="map-canvas"></div>
</div>

<script>
    function initMap() {
        var mapCanvas = document.getElementById("map-canvas");
        var mapOptions = {
            center: new google.maps.LatLng(53.419824, -3.0509294),
            mapTypeId: google.maps.MapTypeId.SATELLITE,
            scrollwheel: true
        }
        var map = new google.maps.Map(mapCanvas, mapOptions);
    }   
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDpgrLi55xM68AaSJOQPX5nzEOV7sw4KVY&callback=initMap"></script>

I think the problem is in how you've described the new map in your mapOptions variable. 我认为问题在于您如何在mapOptions变量中描述新地图。 Try changing the syntax to something like this, passing mapCanvas as the first argument and the options as the second argument: 尝试将语法更改为以下形式,将mapCanvas作为第一个参数,并将options用作第二个参数:

  var mapOptions = new google.maps.Map(mapCanvas, {
            center: {lat: 53.419824, lng: -3.0509294}, 
            mapTypeId: 'satellite',
            scrollwheel: true
    });

I hope this helps! 我希望这有帮助!

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

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