Hi to all. I’m trying to implement google maps in my app but I stumbled in error: google is not defined.
I have looked everywhere but nothing.
My code is:
This is my html file, it is named: map.html
<html>
<head>
<ion-header>
<ion-navbar>
<ion-title>map</ion-title>
</ion-navbar>
</ion-header>
</head>
<body>
<ion-content padding>
<div id="map"></div>
</ion-content>
</body>
<script src="https://maps.google.com/maps/api/js?key=MY_API_KEY" async defer></script>
</html>
And this is my ts file, its name is: map.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular';
import { google } from 'google-maps';
declare var google: google;
@IonicPage()
@Component({
selector: 'page-map',
templateUrl: 'map.html',
})
export class MapPage {
map: any;
constructor(public navCtrl: NavController, public navParams: NavParams, public platform: Platform) {
}
ionViewDidLoad() {
this.platform.ready().then(() => {
this.initMap();
});
console.log('ionViewDidLoad MapPage');
}
initMap() {
let latLng = new google.maps.LatLng(-34.9290, 138.6010);
let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.map = new google.maps.Map(document.getElementById('map'));
this.map = new google.maps.Map(this.map, mapOptions)
}
}
The error instead is:
ERROR Error: "Uncaught (in promise): ReferenceError: google is not defined [115]/MapPage.prototype.initMap@http://localhost:8100/build/main.js:35:13 [115]/MapPage.prototype.ionViewDidLoad/<@http://localhost:8100/build/main.js:30:13 F</l</t.prototype.invoke@http://localhost:8100/build/polyfills.js:3:14974 onInvoke@http://localhost:8100/build/vendor.js:5134:24 F</l</t.prototype.invoke@http://localhost:8100/build/polyfills.js:3:14901 F</c</r.prototype.run@http://localhost:8100/build/polyfills.js:3:10124 f/<@http://localhost:8100/build/polyfills.js:3:20240 F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15649 onInvokeTask@http://localhost:8100/build/vendor.js:5125:24 F</l</t.prototype.invokeTask@http://localhost:8100/build/polyfills.js:3:15562 F</c</r.prototype.runTask@http://localhost:8100/build/polyfills.js:3:10815 o@http://localhost:8100/build/polyfills.js:3:7887 F</h</e.invokeTask@http://localhost:8100/build/polyfills.js:3:16823 p@http://localhost:8100/build/polyfills.js:2:27646 v@http://localhost:8100/build/polyfills.js:2:27893 " chttp://localhost:8100/build/polyfills.js:3:19752fhttp://localhost:8100/build/polyfills.js:3:20273invokeTaskhttp://localhost:8100/build/polyfills.js:3:15649onInvokeTaskhttp://localhost:8100/build/vendor.js:5125:24invokeTaskhttp://localhost:8100/build/polyfills.js:3:15562runTaskhttp://localhost:8100/build/polyfills.js:3:10815ohttp://localhost:8100/build/polyfills.js:3:7887invokeTaskhttp://localhost:8100/build/polyfills.js:3:16823phttp://localhost:8100/build/polyfills.js:2:27646vhttp://localhost:8100/build/polyfills.js:2:27893
How I can solve the problem? Thanks to all