要将本地谷歌地图视图嵌入到应用程序中,有两种解决方案:
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();
});
})
}
}
注:
你不能打电话
这个.IITMAP()
在里面
nGunIn()
函数,因为HTML DOM不是以逗号呈现的。所以在离子4中,你需要打电话给它
IONVIEWDIDEenter()。
相反。有关更多信息,请阅读
Ionic 4 and the Lifecycle Hooks in Angular
截图
您可以在这里找到我的源代码:
ionic4-tabs-map