Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Error Cannot find name 'google' in ionic2 when using googlemaps javascript API

I am following Joshua Morony's Getting Started with Google Maps in Ionic 2 video tutorial. I want to use google maps in my application and i end up with a typescript error. this is a part of the pages/home/home.ts file

initMap(){
let latLng= new google.maps.LatLng(6.929848, 79.857407);

let mapOpt={
  center : latLng,
  zoom : 15,
  mapTypeId :google.maps.MapTypeId.ROADMAP
};

this.map= new google.maps.Map(this.mapElement.nativeElement,mapOpt);}

I tried npm install --save @types/googlemaps,

but it still gives me the same typescript error Typescript Error Cannot find name 'google'

like image 575
YD_ Avatar asked Feb 11 '17 07:02

YD_


2 Answers

I solved it by installing:

$npm install @types/googlemaps --save-dev
like image 73
Biranchi Avatar answered Sep 21 '22 11:09

Biranchi


To expand on the answer from @suraj, you should have:

declare var google; 

outside of the class you are trying to use it in.

Just like in the Josh Morony video, I put it beneath the imports but before the class declaration and annotations (@Injectable() and so forth). I suppose it would still work if you put it above the imports or beneath the end of the class (and still outside of the class), if you were so inclined for whatever reason.

like image 35
Douglas Barbin Avatar answered Sep 22 '22 11:09

Douglas Barbin