简体   繁体   English

Angular 2如何在多个组件中使用Google Maps

[英]Angular 2 How to use google maps in multiple components

I'm building Angular4 project that uses google maps api. 我正在建立使用谷歌地图api的Angular4项目。 I've built a component called basemap in which I've included the google api through jsonp (to get rid of cross origin error when using http module) as follows (basemap.component.ts): 我构建了一个名为basemap的组件,其中通过jsonp包含了google api(以消除使用http模块时出现的跨源错误),如下所示(basemap.component.ts):

if (typeof google === 'undefined') {
// google not available so go fetch it
 this.jsonp.get(`https://maps.googleapis.com/maps/api/js?key=MyKEY&callback=JSONP_CALLBACK`)
    .subscribe(res => {
     // google global object available here
    });
} else {
 //google was fetched before and google global object available here
}

and that works pretty good, but the problem when I use this component multiple times in the same page using ngFor 并且效果很好,但是当我在同一页面中使用ngFor多次使用此组件时出现问题

 <div *ngFor="let i of [1,2,3]">  <app-basemap ></app-basemap> </div>

I get the following error in the console You have included the Google Maps API multiple times on this page. This may cause unexpected errors. 我在控制台中收到以下错误。 You have included the Google Maps API multiple times on this page. This may cause unexpected errors. You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

I can't use this component AGM - Angular Google Maps because I'm using leafletjs as the map visualizing api and google mutant plugin to view google base map. 我无法使用此组件AGM-Angular Google Maps,因为我将leafletjs用作地图可视化api和google突变插件来查看google基本地图。

for any one who come searching for the solution, I've managed to solve the problem using RXJS: I've created a service google-api.service.ts in as the following: 对于任何寻求解决方案的人,我已经设法使用RXJS解决了问题:我创建了一个服务google-api.service.ts,如下所示:

import { Injectable } from '@angular/core';
import { Jsonp, Headers } from '@angular/http';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/retry';

@Injectable()
export class GoogleAPIService {
private googleReadyObservable;
constructor(
   private jsonp: Jsonp
) {
  this.googleReadyObservable = new Subject();
  this.jsonp
  .get(`https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=JSONP_CALLBACK`)
  .retry()
  .subscribe(res => {
    if (res.status === 200) {
      this.googleReadyObservable.complete();
    }
  });
 };

googleReady() {
return this.googleReadyObservable;
  };
}

then in any component foo.component.ts import the service file then inject it in the constructor 然后在任何组件foo.component.ts中导入服务文件,然后将其注入构造函数中

constructor ( 
  private googleAPIService: GoogleAPIService
 ){  }

then use it in any function as following: 然后在以下任何函数中使用它:

this.googleAPIService.googleReady()
    .subscribe(
  null,
  err => console.log(err),
  () => {
     // google maps global object loaded
  }
  );

在index.html中为URL提供密钥

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

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