20

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'

2
  • 4
    The problem with the 2 existing answers is that they defeat the point of having npm install --save @types/googlemaps. You are saying you have this, they are saying use any. It doesn't solve the problem. You can even right click in your IDE and see the google declarations, but Ionic2 doesn't see it. Commented Mar 7, 2017 at 11:35
  • It worked to me in Ionic 3,refer this answer stackoverflow.com/questions/51084724/… Commented Jun 3, 2019 at 18:00

6 Answers 6

25

I solved it by installing:

$npm install @types/googlemaps --save-dev
Sign up to request clarification or add additional context in comments.

3 Comments

Worked for me 2 :)
@Eli I stuck with the same problem, can u help me.
That repo moved. Its now npm i @types/google.maps
15

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.

Comments

11
npm install --save-dev @types/googlemaps

import {} from '@types/googlemaps';

from this answer

Or

in your component.ts file of your page declare it like this

declare var google; before export class component {}

An easy way Angular 6+ available in typescript is:

You only have to add this line at the beginning (meaning line 1, with nothing before) of your Typescript file :

/// <reference types="@types/googlemaps" />

And then

import {RemainingItems} from 'whicheverfolderyouwant';

Updated from

Comments

8

You have to install types:

npm install --save-dev @types/googlemaps

And in your Typescript file where you use the namespace 'google':

[Angular 6+] Add this line at the beginning:

/// <reference types="@types/googlemaps" />

[Angular 5-] Add this line:

import {} from '@types/googlemaps';

from this answer

Comments

2

Newer version!

@types/googlemaps has been deprecated, instead, you need to install a different library:

npm install @types/google.maps

and at the very first line in your file, you will need to add a triple-slash directive:

/// <reference types="@types/google.maps" />

1 Comment

I'm at Angular v14 w "@types/google.maps": "^3.51.0". Followed exactly instruction, const map = new google.maps.DistanceMatrixService(); still receives TS2663: Cannot find name 'google'.
0

No need to do some thing extra Just go into index.d.ts and paste this before declare namespace google.maps

declare module 'googlemaps';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.