简体   繁体   English

谷歌地图没有显示在嵌套的div

[英]google maps not showing in nested div

I am trying to insert a google map into a nested div. 我正在尝试将Google地图插入嵌套的div中。 I copied some generic code from an example i found online to confirm that i could get it to work. 我从网上找到的示例中复制了一些通用代码,以确认可以正常工作。 I pasted that code within the body element of my index.html and it worked fine. 我将该代码粘贴到index.html的body元素中,效果很好。 i then pasted the same code into my app.component.html and the map doesnt show. 然后,我将相同的代码粘贴到我的app.component.html中,地图未显示。 i made sure to use the height and width size in pixels so it wouldnt use any relative sizing for the map container div. 我确保使用以像素为单位的高度和宽度大小,以便它不会对地图容器div使用任何相对大小。 further, when i inspect the element that contains the map, it highlights a large empty square on the screen where the map should be with the same size dimension that i specified. 此外,当我检查包含地图的元素时,它会在屏幕上突出显示一个大的空方块,其中地图应具有我指定的相同尺寸尺寸。 i also noticed when looking at the map element in the dev tools, it has not generated any of the extra nested divs like the known good map div does. 我还注意到,在开发工具中查看map元素时,它没有像已知的好map div那样生成任何额外的嵌套div。

code snippet i am using: 我正在使用的代码段:

<div style="height:100%; width: 100%">
    <h1>My First Google Map</h1>
    <div id="googleMap2" style="height:400px; width: 400px">insert map</div>
    <script>
        function initMap() {
            var mapProp2= {
                center:new google.maps.LatLng(-33.8478796,150.791894), zoom:17,
            };
            var map=new google.maps.Map(document.getElementById("googleMap2"),mapProp2);
        }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap"></script>
</div>

EDIT: i am using an api key, i only inserted API_KEY as a place holder. 编辑:我正在使用一个api密钥,我只插入API_KEY作为占位符。

this image shows what it looks like in my browser 该图像显示了我的浏览器的外观 一种

Add this to your index.html inside your Application <script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"> </script> and inside app.component.html 将此添加到应用程序<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"> </script>和app.component.html内部的index.html中。

import { Component, OnInit } from '@angular/core';
declare var google:any;
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

ngOnInit(){
this.getMap();
}

getMap() {

  let mapProp = {
    center: new google.maps.LatLng(23.4866, 80.1066),
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true,
    draggable: false
  };
  new google.maps.Map(document.getElementById('gmap'), mapProp);

}
}

You need to register and define your API key for the domain you are using, so: 您需要为正在使用的域注册并定义API密钥,因此:

https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap

Should look something like: 应该看起来像:

https://maps.googleapis.com/maps/api/js?key=AzOaSyAI_FJ4STEHLG2SlqU8QCilw_Y9_11W8qc&callback=initMap

It is important to note that the Google JS API is no longer free; 请务必注意,Google JS API不再免费。 so, unless you need to customise your map with multiple markers, etc, that it is better to use the iframe embed feature available when you use the share a map feature in google maps. 因此,除非您需要使用多个标记等自定义地图,否则在Google地图中使用共享地图功能时最好使用可用的iframe嵌入功能。

If you are getting the following ERROR: InvalidKeyMapError then you need get a Api key if already had one then 如果出现以下错误:InvalidKeyMapError,则需要获取Api密钥(如果已经有)

Enable Api do this 启用Api即可

  1. Go to API Manager 转到API管理器
  2. Click on Overview 点击概述
  3. Search for Google Maps JavaScript API(Under Google Maps APIs). 搜索Google Maps JavaScript API(在Google Maps API下)。 Click on that 点击那个
  4. You will find Enable button there. 您将在此处找到“启用”按钮。 Click to enable API. 单击以启用API。

Hope this will solve the problem of enabling API 希望这能解决启用API的问题

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

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