Чтобы встроить собственные виды Google Maps в ваше приложение, существует 2 решения:
1 - Использование элемента DIV ID:
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
import { Platform, NavController } from "@ionic/angular";
import { GoogleMaps, GoogleMap, GoogleMapsEvent, LatLng, MarkerOptions, Marker } from "@ionic-native/google-maps/ngx";
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page implements OnInit {
map: GoogleMap;
constructor(public googleMaps: GoogleMaps, public platform: Platform,
public nav: NavController) { }
async ngOnInit() {
await this.platform.ready();
await this.initMap();
}
initMap() {
this.map = GoogleMaps.create("map_canvas");
this.map.one(GoogleMapsEvent.MAP_READY).then((data: any) => {
let coordinates: LatLng = new LatLng(36.06731743465648, -79.79521393775941);
let position = {
target: coordinates,
zoom: 17
};
this.map.animateCamera(position);
let markerOptions: MarkerOptions = {
position: coordinates,
//icon: "../../assets/images/icons8-Marker-64.png",
title: 'Greensboro, NC'
};
const marker = this.map.addMarker(markerOptions)
.then((marker: Marker) => {
marker.showInfoWindow();
});
})
}
}
2- Использование @ViewChild:
import { Component, ViewChild, OnInit, ElementRef } from '@angular/core';
import { Platform, NavController } from "@ionic/angular";
import { GoogleMaps, GoogleMap, GoogleMapsEvent, LatLng, MarkerOptions, Marker } from "@ionic-native/google-maps/ngx";
@Component({
selector: 'app-tab1',
templateUrl: 'tab1.page.html',
styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
@ViewChild('map') element: ElementRef;
map: GoogleMap;
constructor(public googleMaps: GoogleMaps, public platform: Platform,
public nav: NavController) { }
ionViewDidEnter() {
console.log("call ionViewDidLoad");
this.platform.ready().then(() => {
this.initMap();
});
}
initMap() {
this.map = GoogleMaps.create(this.element.nativeElement);
this.map.one(GoogleMapsEvent.MAP_READY).then((data: any) => {
let coordinates: LatLng = new LatLng(36.06731743465648, -79.79521393775941);
let position = {
target: coordinates,
zoom: 17
};
this.map.animateCamera(position);
let markerOptions: MarkerOptions = {
position: coordinates,
//icon: "../../assets/images/icons8-Marker-64.png",
title: 'Greensboro, NC'
};
const marker = this.map.addMarker(markerOptions)
.then((marker: Marker) => {
marker.showInfoWindow();
});
})
}
}
Примечание: Вы не можете вызвать this.initMap () в функции ngOnInit () потому что HTML DOM не отображается полностью.Поэтому в Ionic 4 вам нужно вызывать его в ionViewDidEnter () .Для получения дополнительной информации, пожалуйста, прочитайте Ionic 4 и крюки жизненного цикла в угловом
Снимок экрана
![enter image description here](https://i.stack.imgur.com/WLla1.png)
Вы можете найти мой исходный код здесь: ionic4-tabs-map