
备注此项目必须在设备iOS或Andriod上运行
背景:
我已经使用命令$ ionic plugin add cordova-plugin-googlemaps --variable API_KEY_FOR_ANDROID="YOUR_ANDROID_API_KEY_IS_HERE" --variable API_KEY_FOR_IOS="YOUR_IOS_API_KEY_IS_HERE"将本机google插件安装到我的离子型2项目中,如下所示:http://ionicframework.com/docs/v2/native/google-maps/
然后运行命令$ ionic platform add ios,然后运行$ ionic build ios
直到现在一切都如期而至。当我试图显示一张地图,我看到一个黑色的屏幕,不知道什么错过了!
代码:
/src/app/app.module.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}]
})
export class AppModule {}/src/app/app.component.ts
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { HomePage } from '../pages/home/home';
import {
GoogleMap,
GoogleMapsEvent,
GoogleMapsLatLng,
CameraPosition,
GoogleMapsMarkerOptions,
GoogleMapsMarker
// GoogleMapsMapTypeId
} from 'ionic-native';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage = HomePage;
constructor(platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
Splashscreen.hide();
let map = new MapPage();
map.loadMap();
});
}
}
class MapPage {
constructor() {}
// Load map only after view is initialize
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 = new GoogleMap(element);
// create LatLng object
let ionic: GoogleMapsLatLng = new GoogleMapsLatLng(43.0741904,-89.3809802);
// create CameraPosition
let position: CameraPosition = {
target: ionic,
zoom: 18,
tilt: 30
};
// listen to MAP_READY event
map.one(GoogleMapsEvent.MAP_READY).then(() => {
// move the map's camera to position
map.moveCamera(position); // works on iOS and Android
});
// create new marker
let markerOptions: GoogleMapsMarkerOptions = {
position: ionic,
title: 'Ionic'
};
map.addMarker(markerOptions)
.then((marker: GoogleMapsMarker) => {
marker.showInfoWindow();
});
}
}/src/page/home/home.home
<ion-header>
<ion-navbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<div #map id="map" style="height:100%;"></div>
</ion-content>有人能帮我解决这个问题吗。我们对你的帮助深表感谢。
链接到文档
发布于 2017-03-13 13:42:58
let map = new MapPage();
map.loadMap();`MapPage是一个普通的类型记录类。它不是一个组件,它不会像这样运行生命周期挂钩:
ngAfterViewInit() {
this.loadMap();
}另外,由于元素位于map.loadMap()中,所以App.html中的调用home component无法工作。
我建议您将映射相关代码移动到home组件。还可以使用viewChild获取map div,这将是一个ElementRef对象。
家庭组件
constructor() {}
@ViewChild('map')
private mapElement: ElementRef;
// Load map only after view is initialize
ngAfterViewInit() {
this.loadMap();
}
loadMap() {
let map = new GoogleMap(this.mapElement.nativeElement);
//...etc etc
}
}发布于 2017-05-05 13:16:01
在app.scss中,添加:
ion-app._gmaps_cdv_ .nav-decor{
background: none !important;
}发布于 2017-05-26 08:55:40
问题:在iOS平台上加载地图不起作用。它显示的是黑色屏幕。然而,地图请求在Google控制台上是正确更新的。
解决方案:我必须在.scss文件中添加以下代码-
ion-app._gmaps_cdv_ .nav-decor{
display: none !important;
}这是正确的工作和加载地图。
https://stackoverflow.com/questions/42764139
复制相似问题