angular - ionic 3 : Uncaught (in promise): ReferenceError: google is not defined ReferenceError

标签 angular google-maps typescript ionic3

你好吗?我正在研究带有 Ionic 3 教程的 Google map 。我已经完成了那里解释的一切,但是当项目启动时,出现了这个错误。我调查了很多,但没有任何效果。谢谢!

Error: Uncaught (in promise): ReferenceError: google is not defined
ReferenceError: google is not defined

这是我的代码:

home.ts

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import { IonicPage } from 'ionic-angular';

declare var google;

@IonicPage()
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {

    @ViewChild('map') mapElement:ElementRef;
    map: any;
    start = 'chicago, il';
    end = 'chicago, il';
    directionsService = new google.maps.DirectionsService;
    directionsDisplay = new google.maps.DirectionsRenderer;

    constructor(public navCtrl: NavController) {

    }

    ionViewLoad(){
        this.initMap();
    }

    initMap(){
        this.map = new google.maps.Map(this.mapElement.nativeElement, 
    { 
            zoom: 7,
            center: {lat: 41.85, lng: -87.65}
    });

        this.directionsDisplay.setMap(this.map);
    }

    calculateAndDisplayRoute(){
        this.directionsService.route({
            origin: this.start,
            destination: this.end,
            travelMode: 'DRIVING',
        }, (response, status) => {
            if(status == 'OK'){
                this.directionsDisplay.setDirections(response);
            }else{
                window.alert('Directions request failed due to ' + 
        status);
            }
        });
        }

     }

最佳答案

确保在加载 javascript 时使用 HTTPS!

由此改变

<script src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>

关于angular - ionic 3 : Uncaught (in promise): ReferenceError: google is not defined ReferenceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313492/

相关文章:

Angular 2上下滑动动画

javascript - 将脚本添加到 Angular 库

google-maps - 谷歌地图 API 风格的 map 与卫星水

javascript - 在 TypeScript 中将数字转换为字符串

typescript :有递归keyof吗?

ionic-framework - 如何将 Ionic2 更新到最新/master Angular2?

angular - Angular2中如何实现拖拽?

javascript - Angular Reactive Forms 验证的奇怪错误

javascript - Google map JS API - 未定义 'point.pageX'

ios - 即使我的 iOS 设备中没有谷歌地图,“canOpenURL”也会返回 true?