【问题标题】:Angular2/ionic2 angular2-google-maps error Cannot find name 'google'Angular2/ionic2 angular2-google-maps 错误找不到名称“google”
【发布时间】:2017-04-08 16:30:35
【问题描述】:

我跟着this example (click here)做了一个地址字段,自动完成谷歌地图的地方,

但它给出了以下错误:

Can not find name 'google'.

L53: this.mapsAPILoader.load (). Then (() => {

L54: let autocomplete = new google.maps.places.Autocomplete 
(this.searchElementRef.nativeElement, {

我尝试安装 google-maps 类型 npm install --save @types/google-maps 但没有结果。

安装@types/google-maps 后构建正常,但是当我 luanch 时出现此错误:

Cannot find name 'google' after installing @types/google-maps

我的代码:

import { FormControl } from '@angular/forms';
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { MapsAPILoader } from 'angular2-google-maps/core';


@Component({
  selector: 'page-page2',
  templateUrl: 'page2.html'
})
export class Page2 implements OnInit {

  latitude: number = 51.678418;
  longitude: number = 7.809007;
  zoom: number = 4;

  searchControl: FormControl;

  @ViewChild("search")
  searchElementRef: ElementRef;

  constructor(public navCtrl: NavController, public navParams: NavParams, private mapsAPILoader: MapsAPILoader) {
// some not related to this question code
  }

  ngOnInit() {
    //create search FormControl
    this.searchControl = new FormControl();

    //set current position
    this.setCurrentPosition();

    //load Places Autocomplete
    this.mapsAPILoader.load().then(() => {
      let autocomplete = new google.maps.places.Autocomplete(this.searchElementRef.nativeElement, {
        types: ["address"]
      });
      autocomplete.addListener("place_changed", () => {
        //get the place result
        let place: google.maps.places.PlaceResult = autocomplete.getPlace();

        //set latitude and longitude
        this.latitude = place.geometry.location.lat();
        this.longitude = place.geometry.location.lng();
      });
    });
  }

  private setCurrentPosition() {
    if ("geolocation" in navigator) {
      navigator.geolocation.getCurrentPosition((position) => {
        this.latitude = position.coords.latitude;
        this.longitude = position.coords.longitude;
        this.zoom = 12;
      });
    }
  }

}

【问题讨论】:

  • 将declare var google; 放在imports 下方和@Component({}) 上方会发生什么?并请提供一些代码
  • 感谢您的反馈。我已经添加了我的代码
  • 你能试试npm install --save @types/google-maps吗?
  • 我已经这样做了。我收到以下错误i.stack.imgur.com/gPZzD.png
  • 好的,然后试试typings install dt~google.maps --global

标签: google-maps angular autocomplete ionic2 google-places-api


【解决方案1】:

运行typings install dt~google.maps --global

如发现here

【讨论】:

  • 对我来说,在执行您给我的这两条指令后问题就解决了:1. npm install --save @types/google-maps 2. typings install dt~google.maps - -global 谢谢@Ivaro18
猜你喜欢
  • 1970-01-01
  • 2017-07-12
  • 2017-06-10
  • 2017-05-23
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-29
相关资源
最近更新 更多