Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic why nativeElement of undefined?

Tags:

I have an ionic application using segments, and on one segment I want to display google maps. When we load this segment first it works but when I go to another segment and I want to go back to the google maps segment, I get an error message. And I do not know how to solve this problem.

TypeError: Cannot read property 'nativeElement' of undefined

HTML

<ion-header>
    <ion-toolbar>
      <ion-segment [(ngModel)]="gps" color="light">
        <ion-segment-button value="information" (click)="onInformationClick()">
          <ion-icon name="information-circle"></ion-icon>
        </ion-segment-button>
        <ion-segment-button value="navigate" (click)="onNavigateClick()">
          <ion-icon name="navigate"></ion-icon>
        </ion-segment-button>
        <ion-segment-button value="settings" (click)="onSettingsClick()">
            <ion-icon name="settings"></ion-icon>
          </ion-segment-button>
      </ion-segment>
    </ion-toolbar>
  </ion-header>


<ion-content class="home">
        <div [ngSwitch]="gps" id="contenu">
            <div *ngSwitchCase="'information'"><h1>information</h1></div>


            <div #map id="map" *ngSwitchCase="'navigate'"></div>


            <div *ngSwitchCase="'settings'"><h1>settings</h1></div>
        </div>

</ion-content>

TypeScript

import { Component ,ViewChild, ElementRef} from '@angular/core';
import { NavController, Platform} from 'ionic-angular';
import { AndroidPermissions } from '@ionic-native/android-permissions';

declare var SMS:any;
declare var google:any;
@Component({
  selector: 'page-informations',
  templateUrl: 'informations.html'
})
export class InformationsPage {
  //mySMS:any[]=[];
  gps: string = "navigate";
  @ViewChild('map') mapRef:ElementRef;

  constructor(
    public navCtrl: NavController, 
    public androidPermissions: AndroidPermissions, 
    public platform:Platform) 
  {
// constructor 
}
  public onNavigateClick(){
    this.gps = "navigate";
    this.DisplayMap(); 
  }
  public onInformationClick(){
    this.gps = "information";
  }

  public onSettingsClick(){
    this.gps = "settings";
  }
 //Lance l'affichage de la map'

//Définition des paramètre de la map
 DisplayMap() { 
  //Coordonnée de la zone de départ
  const location = new google.maps.LatLng(49.898738,
    1.131385);

  // Options sur la coordonnée de départ
  const options = {
    center:location,
    zoom:19,
    streetViewControl:false,
    mapTypeId:'hybrid'
  };

  // Coordonnées pour chaque points de gps récupéré
  const coordinates =[
    {lat: 80.898613, lng: 1.131438},
    {lat: 80.898501, lng: 1.131355},
    {lat:80.898698, lng: 1.130996},
    {lat: 80.898822, lng: 1.131206}
  ];

  //Option des points de coordonnée gps récupéré
  const flightPath = new google.maps.Polyline({
    path: coordinates,
    geodesic: true,
    strokeColor: '#FBE625',
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

  //Déclaration de la map
  const map = new google.maps.Map(this.mapRef.nativeElement,options);

  //Ajout des points de coordonnées gps récupéré, sur la map
  flightPath.setMap(map);
}
}
like image 692
Kol2gaR Avatar asked Jan 05 '18 14:01

Kol2gaR


1 Answers

you are trying to get the value from html before rendering completely. create a timeout inside ngAfterViewInit to wait until the values get rendered.

const map;
ngAfterViewInit() {
    console.log("afterinit");
    setTimeout(() => {
      console.log(this.mapRef.nativeElement);
      map = new google.maps.Map(this.mapRef.nativeElement,options);
    }, 1000);
  }
like image 76
Sachila Ranawaka Avatar answered Sep 23 '22 12:09

Sachila Ranawaka