2
votes

Uncaught (in promise):

TypeError: Cannot read property 'firstChild' of null TypeError: Cannot read property 'firstChild' of null at Object._.og (maps.googleapis.com/maps/api/js?key=&callback=initMap:88:391) at new tg (maps.googleapis.com/maps/api/js?key=&callback=initMap:90:76) at ParkMapPage.initializeMap

Above Error when I try ionic serve --lab

import { Component } from '@angular/core';
import { Platform, NavController } from 'ionic-angular';


@Component({
  selector: 'page-park-map',
  templateUrl: 'park-map.html'
})

export class ParkMapPage {
  map: google.maps.Map;

  constructor(public navCtrl: NavController, public platform: Platform) {
    this.map = null;
    this.platform.ready().then(() => {
      this.initializeMap();
    });
  }

  initializeMap() {
    let minZoomLevel = 3;

    this.map = new google.maps.Map(document.getElementById("map_canvas"), {
      zoom: minZoomLevel,
      center: {lat: 39.833, lng: -98.583},
      mapTypeControl: false,
      streetViewControl: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  }

}

Above is my code in Typescript file.

<ion-content> <div id="map_canvas"></div> </ion-content>

Above is my code in HTML file.

This is an exercise from a book. I can't solve it, can anyone please help me ):

2

2 Answers

3
votes

I strongly suggest that you use the Ionic Native wrapper for google maps instead of going solo.

1. Install the Cordova and Ionic Native plugins:

$ ionic cordova plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE"
$ npm install --save @ionic-native/google-maps

2. After installing the plugin’s package, add it to your app’s NgModule.

...

import { Camera } from '@ionic-native/camera';

...

@NgModule({
  ...

  providers: [
    ...
    GoogleMaps
    ...
  ]
  ...
})
export class AppModule { }

3. Usage

import {
 GoogleMaps,
 GoogleMap,
 GoogleMapsEvent,
 LatLng,
 CameraPosition,
 MarkerOptions,
 Marker
} from '@ionic-native/google-maps';

export class MapPage {
 constructor(private googleMaps: GoogleMaps) {}

// Load map only after view is initialized
ngAfterViewInit() {
 this.loadMap();
}

loadMap() {
 // make sure to create following structure in your view.html file
 // and add a height (for example 100%) to it, else the map won't be visible
 // <ion-content>
 //  <div #map id="map" style="height:100%;"></div>
 // </ion-content>

 // create a new map by passing HTMLElement
 let element: HTMLElement = document.getElementById('map');

 let map: GoogleMap = this.googleMaps.create(element);

 // listen to MAP_READY event
 // You must wait for this event to fire before adding something to the map or modifying it in anyway
 map.one(GoogleMapsEvent.MAP_READY).then(
   () => {
     console.log('Map is ready!');
     // Now you can add elements to the map like the marker
   }
 );

 // create LatLng object
 let ionic: LatLng = new LatLng(43.0741904,-89.3809802);

 // create CameraPosition
 let position: CameraPosition = {
   target: ionic,
   zoom: 18,
   tilt: 30
 };

 // move the map's camera to position
 map.moveCamera(position);

 // create new marker
 let markerOptions: MarkerOptions = {
   position: ionic,
   title: 'Ionic'
 };

 const marker: Marker = map.addMarker(markerOptions)
   .then((marker: Marker) => {
      marker.showInfoWindow();
    });
 }

}
2
votes

The previous of course is an excellent answer from maninak. As an alternative, to fix this issue in the direct javascript way, as chris griffith presents in his book, see: link to book forum

It's quite simple -- you need to move the call to this.initializeMap(); over to the ionViewDidLoad() function:

ionViewDidLoad() {
    this.initializeMap();
}

This goes inside the ParkMapPage class. And take away the call to initializeMap inside the constructor.

These changes are because of changes coming from Ionic 3 instead of Ionic 2.