简体   繁体   English

Google Maps JavaScript API显示灰色空白框而不是地图

[英]Google Maps JavaScript API shows grey blank box instead of map

I can't seem to get a map to show on an Ionic Application, with Google Maps JavaScript API. 我似乎无法使用Google Maps JavaScript API在Ionic应用程序上显示地图。

So far I have tried google.maps.event.trigger(map, 'resize') , since I have changed the width of the map div... Saw that from a solution here . 到目前为止,我已经尝试过google.maps.event.trigger(map, 'resize') ,因为我更改了地图div的宽度...从这里的解决方案中可以看到 FYI the map used to work fine, but had a few glitches when I tried to pan around. 仅供参考,该地图过去可以正常运行,但是当我尝试平移时出现了一些故障。

In map.component.ts 在map.component.ts中

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { google } from 'google-maps';

@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.scss'],
})
export class MapComponent implements OnInit {
  // Map setup
  @ViewChild('mapElement', {static: true}) mapElement: ElementRef<HTMLElement>;
  map: google.maps.Map;

  constructor() {
  }
  ngOnInit() {
    console.log('Map')
    this.map = new google.maps.Map(this.mapElement.nativeElement, {
      mapTypeId: google.maps.MapTypeId.SATELLITE
    });
    google.maps.event.trigger(this.map, 'resize')
    console.log('Map Seconded')
  }
}

The HTML in map.component.ts map.component.ts的HTML

<div id="map-element" #mapElement></div>

The CSS is used to style the map-element in global.css CSS用于在global.css设置地图元素的样式

#map-element {
    display: block;
    height: 400px;
}

Just so you can compare API Keys here is mine I have a feeling that is where the problem might be, after I have found a solution I'll have it regenerated, AIzaSyB6RgF7vmUE0mjk_glikiuuSmpGpaNiA24 只是为了您可以比较API密钥,这是我的问题,我感觉是问题所在,在找到解决方案后,我将重新生成它, AIzaSyB6RgF7vmUE0mjk_glikiuuSmpGpaNiA24

When I view the app on Google Chrome and try to zoom in and out of the map I get this error ERROR TypeError: Cannot read property 'zoom' of null 当我在Google Chrome浏览器上查看该应用并尝试放大和缩小地图时,出现此错误ERROR TypeError: Cannot read property 'zoom' of null

Personally I needed a location from google maps on my website, so what I did is that I embedded the the location from google maps, and you do so by clicking on "Share" and then "Embed a map" and then you copy the html code and put it in your html code. 我个人需要在我的网站上从Google地图获取位置,所以我要做的就是从Google地图嵌入该位置,然后单击“共享”,然后“嵌入地图”,然后复制HTML代码并将其放在您的html代码中。

<a><iframe src="html code" class="map" width="250" height="200" frameborder="0" 
style="border:0" allowfullscreen></iframe></a>

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

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