我使用Ionic 3版本,我尝试将一个页面添加到我的应用程序中,以显示带有标记的地图。 我已经在我的应用中使用Google地图ID进行自动填充功能(Google地址......)。 我使用了Google API,并将Map Embed,Javascript等添加到了我的API密钥中。 但页面底部显示“Google”,显示按钮“,但地图为空。 见附件......
安装Cordova和Ionic Native插件:
$离子科尔多瓦插件添加https://github.com/mapsplugin/cordova-plugin-googlemaps#multiple_maps --variable API_KEY_FOR_ANDROID = “AIzaSyB6mEnxH4vC +++++++++ 9wnXXNNmK2co” --variable API_KEY_FOR_IOS =“AIzaSyB6mEnxH4v ++++++++++++ ++ wnXXNNmK2co” $ npm install --save @ ionic-native / google-maps
Home.ts:
import { NavController } from 'ionic-angular';
import { Component, ViewChild, ElementRef } from '@angular/core';
import { GoogleMaps, CameraPosition, GoogleMapsEvent, GoogleMap, MarkerOptions, Marker } from "@ionic-native/google-maps";
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
@ViewChild('map') mapElement: ElementRef;
map: any;
constructor(public navCtrl: NavController, private googleMaps: GoogleMaps) {
}
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 CameraPosition
let position: CameraPosition = {
target: {
lat: 43.0741904,
lng: -89.3809802
},
zoom: 18,
tilt: 30
};
// move the map's camera to position
}
}
home.html的
Home.html :
<ion-header>
<ion-navbar>
<ion-title>
Map
</ion-title>
<ion-buttons end>
<button ion-button (click)="addMarker()"><ion-icon name="add"></ion-icon>Add Marker</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<div #map id="map" style="height:100%;"></div>
</ion-content>
Home.scss
page-home {
}
答案 0 :(得分:2)
请勿使用ngAfterViewInit
。
您必须等待platform.ready()
// Wait the native plugin is ready.
platform.ready().then(() => {
this.loadMap();
});
回购:https://github.com/mapsplugin/ionic-google-maps
目前的官方文档页面错误。我发送了拉取请求,但它现在正在等待。 https://github.com/ionic-team/ionic-native/pull/1834
答案 1 :(得分:1)
请尝试以下代码并确保使用正确的API密钥,在.ts文件中编写以下代码:
ionViewDidLoad() {
this.loadMap();
}
loadMap() {
let mapOptions: GoogleMapOptions = {
camera: {
target: {
lat: 43.0741904,
lng: -89.3809802
},
zoom: 18,
tilt: 30
}
};
this.map = this.googleMaps.create('map_canvas', mapOptions);
// Wait the MAP_READY before using any methods.
this.map.one(GoogleMapsEvent.MAP_READY)
.then(() => {
console.log('Map is ready!');
this.map.addMarker({
title: 'Ionic',
icon: 'blue',
animation: 'DROP',
position: {
lat: 43.0741904,
lng: -89.3809802
}
})
.then(marker => {
marker.on(GoogleMapsEvent.MARKER_CLICK)
.subscribe(() => {
alert('clicked');
});
});
});
}
在您的.html文件中定义地图,如下所示
<ion-content>
<div id="map_canvas" style="height: 100%;"></div>
</ion-content>