2

I want to integrate google maps into my app without using the angular2-google-maps component. I tried following the getting started tutorial

Google maps - Hello world

When I put the two scripts from the tutorial in the index.html file and create a maps component, which basically only links to an html file with the <div id="map"></div> tag in it, I get the following error:

TypeError: Cannot read property 'offsetWidth' of null.

This is probably due to the script being executed before the actual div is loaded. How can I make these scripts execute after all the elements have loaded? Adding the scripts to the html part of my maps component doesn't seem to work.

Thanks!

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
Raising
  • 21
  • 1

3 Answers3

0

Try to remove the async attribute from the google maps script tag. If it still doesn't work, you can try using this Synchronous Loading

Good luck.

Shlomi Haver
  • 954
  • 9
  • 14
  • I've tried this, but it isn't working. I think the problem is that the map div is in another component that's loaded after the script. Also the inability to run scripts in the component html is blocking me here. – Raising Jun 25 '16 at 09:27
  • Can you put your code on [js fiddle](https://jsfiddle.net/), it will be much easier for me to help you. – Shlomi Haver Jun 25 '16 at 09:41
0

You can try to use the same approach that lets jQuery run with Angular 2.

Use setTimeout:This let's the jQuery run in the next execution cycle after Angular has finished loading all the child components.

export class HomePage {

constructor() {
    setTimeout(() => {

   // your code here 

    }, 0);
}
}
Community
  • 1
  • 1
KB_
  • 2,113
  • 2
  • 26
  • 28
0

You can try AGM or @angular/google-maps

Personnally I prefer the second options but both link explain well how to implement gmap in an angular app.

Dharman
  • 30,962
  • 25
  • 85
  • 135
mbagiella
  • 256
  • 2
  • 12