I am trying to integrate google map in ionic 4 application, and unfortunately facing nativeElement not found error.
@ViewChild('Map') mapElement: ElementRef; ~~~~~~~~~~~~~~~~
node_modules/@angular/core/core.d.ts:8436:47 8436 (selector: Type | Function | string, opts: { ~~~~~~~ 8437 read?: any; ~~~~~~~~~~~~~~~~~~~ 8438 static: boolean; ~~~~~~~~~~~~~~~~~~~~~~~~ 8439 }): any; ~~~~~ An argument for 'opts' was not provided. System console .
home.page.ts
import {Component, ElementRef, NgZone, ViewChild, OnInit} from '@angular/core';
import {Geolocation} from '@ionic-native/geolocation/ngx';
declare var google: any;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
@ViewChild('Map') mapElement: ElementRef;
map: any;
mapOptions: any;
location = {lat: null, lng: null};
markerOptions: any = {position: null, map: null, title: null};
marker: any;
apiKey: any = 'API_KEY';
constructor(public zone: NgZone, public geolocation: Geolocation) {
/*load google map script dynamically */
const script = document.createElement('script');
script.id = 'googleMap';
if (this.apiKey) {
script.src = 'https://maps.googleapis.com/maps/api/js?key=' + this.apiKey;
} else {
script.src = 'https://maps.googleapis.com/maps/api/js?key=';
}
document.head.appendChild(script);
/*Get Current location*/
this.geolocation.getCurrentPosition().then((position) => {
this.location.lat = position.coords.latitude;
this.location.lng = position.coords.longitude;
});
/*Map options*/
this.mapOptions = {
center: this.location,
zoom: 21,
mapTypeControl: false
};
setTimeout(() => {
this.map = new google.maps.Map(this.mapElement.nativeElement, this.mapOptions);
/*Marker Options*/
this.markerOptions.position = this.location;
this.markerOptions.map = this.map;
this.markerOptions.title = 'My Location';
this.marker = new google.maps.Marker(this.markerOptions);
}, 3000);
}
}