将 googlemaps 添加到 angular-cli [英] adding googlemaps to angular-cli

查看:23
本文介绍了将 googlemaps 添加到 angular-cli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 googlemaps 实施到我的 angular-cli 项目中.我知道有一个angular2-google-maps"组件(github),但我需要路由和更多自定义功能.

I am trying to implement googlemaps to my angular-cli project. I know that there is a 'angular2-google-maps' component (github), but I need routes and more custom features.

我找到了两种将地图实施到项目中的方法:

I found two ways on implementing the map into an project:

1:stackoverflow:使用谷歌api 加载器 - 但我不知道如何初始化谷歌地图...

1: stackoverflow: with a google api loader - but I couldn't figure out how to initialise google-maps...

2:使用绝对类型 google.maps.d.ts.我将它与 --global (因为环境不推荐使用..)一起安装到我的项目中,并将 index.d.ts(用于全局)添加到 src/typings.d.ts 并且可以使用google.map..".ts 文件:

2: with an DefinitelyTyped google.maps.d.ts. I installed it with --global (since ambient ist deprecated..) into my project and added the index.d.ts (for global) to the src/typings.d.ts and can use "google.map.." in an .ts file:

  myLatLng = {lat: -25.363, lng: 131.044};
  map:any;

  constructor() {
    this.map = new google.maps.Map(document.getElementById('map'), {
      center: this.myLatLng,
      zoom: 4
    });
  }

但是如果我用 angular-cli 构建它会出错:参考错误:未定义谷歌"

but if I build it with angular-cli it errors: "ReferenceError: google is not defined"

有什么想法吗?

推荐答案

这里是将谷歌地图组件添加到 angular-cli 项目的分步指南.

Here a step by step guide to add a google maps component to angular-cli project.

步骤 1:从绝对类型安装 google.maps:

Step1 : install google.maps from DefinitelyTyped:

typings i dt~google.maps --global --save

第 2 步:如果您安装了较旧的类型,请添加

Step2 : if you have an older typings installed add

/// <reference path="../typings/index.d.ts" />

到你的 src/typings.d.ts

to your src/typings.d.ts

Step3:生成新组件

Step3 : generate new component

ng g component google-maps

将以下代码添加到:

.ts :

  height = '100px';
  myLatLng = {lat: -25.363, lng: 131.044};
  map:any;

  constructor(private googleApi:GoogleApiService) {}

  ngOnInit() {
    this.googleApi.initMap().then(() => {

      let latlng = new google.maps.LatLng(this.myLatLng.lat, this.myLatLng.lng);

      this.map = new google.maps.Map(document.getElementById('map'), {
        center: latlng,
        zoom: 4
      });

      new google.maps.Marker({
        position: latlng,
        map: this.map,
        title: 'Hello World!'
      });
    });
  }

.html:

<div id="map" [style.height]="height"></div>

Step4 : 生成新服务

Step4 : generate new service

ng g service google-maps/shared/google-api

将 GoogleApiService + HTTP_PROVIDERS 添加到 src/main.ts

Add GoogleApiService + HTTP_PROVIDERS to src/main.ts

代码:

const API_KEY = '[insert your code]';
const url = 'https://maps.googleapis.com/maps/api/js?key='+ API_KEY +'&callback=initMap';

@Injectable()
export class GoogleApiService {
  private loadMap: Promise<any>;

  constructor(private http:Http) {
    this.loadMap = new Promise((resolve) => {
      window['initMap'] = () => {
        resolve();
      };
      this.loadScript()
    });
  }

  public initMap():Promise<any> {
    return this.loadMap;
  }

  private loadScript() {
    let script = document.createElement('script');
    script.src = url;
    script.type = 'text/javascript';

    if (document.body.contains(script)) {
      return;
    }
    document.getElementsByTagName('head')[0].appendChild(script);
  }
}

也许你需要从 spec.ts 文件中删除一些行.但随后您可以将 GoogleMapsComponent 添加为指令.

Maybe you need to delete some lines from the spec.ts files. But then you can add the GoogleMapsComponent as a directive.

  • 使用路由等进行扩展非常容易.也许如果我有时间,我会将当前版本的 GoogleMapsComponent 上传到 github..

这篇关于将 googlemaps 添加到 angular-cli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆