angular - Angular Google Maps 模块在本地渲染,但不在生产环境中
问题描述
我有一个使用官方 Angular Google Maps 模块的小应用程序。它在我的本地环境中创造了奇迹,但在生产中它只会显示一个灰色窗口。检查代码,地图、标记和信息窗口都在那里,但没有呈现任何内容。
在生产中,加载时没有错误,但是当我尝试放大地图时出现以下错误:
zone-evergreen.js:171 Uncaught TypeError: Cannot read property 'zoom' of null
at ev (map.js:4)
at HTMLDivElement.<anonymous> (map.js:3)
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Zone.runTask (zone-evergreen.js:167)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:480)
at invokeTask (zone-evergreen.js:1621)
at HTMLDivElement.globalZoneAwareCallback (zone-evergreen.js:1647)
这是我的代码:
import { MapInfoWindow } from '@angular/google-maps'
export class ExhibitorDetailsComponent implements OnInit, AfterViewInit {
// Map stuffs
@ViewChild(MapInfoWindow, { static: false }) infoWindow: MapInfoWindow
openInfo(marker: MapMarker, content) {
this.infoWindow.open(marker)
}
zoom = 14
center: google.maps.LatLngLiteral
options: google.maps.MapOptions = {
mapTypeId: 'roadmap',
zoomControl: true,
scrollwheel: true,
disableDoubleClickZoom: false,
maxZoom: 15,
minZoom: 8,
mapTypeControl: true,
scaleControl: true,
streetViewControl: true,
rotateControl: true,
fullscreenControl: true,
}
markers = [{
position: {
lat: 52.5504827,
lng: 13.4015135,
},
title: 'Marker title ',
clickable: true,
options: { },
}]
lat = this.markers[0].position.lat;
lng = this.markers[0].position.lng;
// Open marker info window on load
@ViewChild('markerElem') markerElem;
ngAfterViewInit() {
navigator.geolocation.getCurrentPosition(position => {
this.center = {
lat: 52.5504827,
lng: 13.4015135,
}
})
this.openInfo(this.markerElem, "");
}
// On Init
ngOnInit(): void {
this.route.paramMap.subscribe(params => {
let item = params.get('exhibitorId')
console.log(item);
this.exhibitor = exhibitors.find(x => x.doc.slug === item);
console.log(this.exhibitor);
});
}
}
在前面:
<google-map height="100%" width="100%" [zoom]="zoom" [center]="center" [options]="options">
<map-info-window>{{exhibitor.doc.galleryname}} <br><a href="https://www.google.com/maps/dir//'+{{lat}},{{lng}}'/@52.5504827,13.4015135,13z/" target="_blank">Get direction</a></map-info-window>
<map-marker class="marrrker" id="markerclick" #markerElem *ngFor="let marker of markers" [position]="marker.position" [label]="marker.label" [title]="marker.title" [options]="marker.options" (mapClick)="openInfo(markerElem, '')">
</map-marker>
</google-map>
到目前为止我所尝试的:
- 这里提到的一切。包含:
- 降级到 9.1.3
- 使用定义的 mapTypeId 进行初始化
- 没有隐藏容器中的地图
- 彻底改变容器和 CSS
关于这个谜团的任何指示?
编辑:尝试王子的回答
这就是它现在的样子。还是同样的问题!
import { Component, OnInit, Input, ViewChild, AfterViewInit,ElementRef, Renderer2 } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { GoogleMapsModule } from '@angular/google-maps'
import { MapMarker } from '@angular/google-maps'
import { MapInfoWindow } from '@angular/google-maps'
@Component({
selector: 'app-exhibitor-details',
templateUrl: './exhibitor-details.component.html',
styleUrls: ['./exhibitor-details.component.scss']
})
export class ExhibitorDetailsComponent implements OnInit, AfterViewInit {
// Map stuffs
@ViewChild(MapInfoWindow, { static: false }) infoWindow: MapInfoWindow
openInfo(marker: MapMarker, content) {
this.infoWindow.open(marker)
}
'zoom' = 14
'mapTypeId': google.maps.MapTypeId;
'center': {
'lat': 52.5504827,
'lng': 13.4015135
}
'options': google.maps.MapOptions = {
'zoomControl': true,
'scrollwheel': true,
'disableDoubleClickZoom': false,
'maxZoom': 15,
'minZoom': 8,
'mapTypeControl': false,
'scaleControl': false,
'streetViewControl': false,
'rotateControl': false,
'fullscreenControl': false,
}
'markers' = [{
'position': {
'lat': 52.5504827,
'lng': 13.4015135,
},
'title': 'Marker title ',
'clickable': true,
'options': { },
}]
'lat' = this.markers[0].position.lat;
'lng' = this.markers[0].position.lng;
// Open marker info window on load
@ViewChild('markerElem') markerElem;
ngAfterViewInit() {
setTimeout(() => {
this.mapTypeId = google.maps.MapTypeId.ROADMAP;
}, 3000);
setTimeout(() => {
navigator.geolocation.getCurrentPosition(position => {
this.center = {
'lat': 52.5504827,
'lng': 13.4015135,
}
})
}, 3000);
this.openInfo(this.markerElem, "");
}
// On Init
ngOnInit(): void {
navigator.geolocation.getCurrentPosition(position => {
this.center = {
'lat': 52.5504827,
'lng': 13.4015135,
}
})
}
}
解决方案
存在 settimeout 的代码部分存在问题!
this.openInfo(this.markerElem, "")
将在您的两个 setTimeouts 中的代码之前很久执行!
而不是 ngAfter ngAfterViewInit
,试试这个:
isMapInitiated = false;
...
ngAfterViewChecked() {
if( !this.isMapInitiated ) {
setTimeout(() => {
this.mapTypeId = google.maps.MapTypeId.ROADMAP;
navigator.geolocation.getCurrentPosition(position => {
this.center = {
'lat': 52.5504827,
'lng': 13.4015135,
}
this.openInfo(this.markerElem, "");
this.isMapInitiated = true;
}, 3000);
}
}
这3000
取决于对必要的 Google JS 文件收费的级别!如果它在组件启动之前充电,您可以将其设置为0
。
推荐阅读
- ruby-on-rails - Rails网站登录在生产中不起作用(Heroku)
- c++ - 如何为对象提供不同的接口(最佳)
- git - 为什么 .git 目录在不同的 GIT_DIR 中!分支?
- sql - 比较两个 select 语句的结果
- android - 使用库 (https://github.com/OmarAflak/Bluetooth-Library) 制作的蓝牙应用程序崩溃
- c - 如何在for循环中输入if语句?
- c# - 如何在不暴露明文的情况下通过网络传输 SecureString(或类似内容)?
- expression - 使用通用表达式语言 (CEL) 在列表中查找值的示例
- python - 将 Python 代码与 MySQL 连接时出现回溯错误
- javascript - 如何将多个元素插入多维数组的同一索引中?