首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >它显示了黑色屏幕时,试图加载地图在设备上的离子2谷歌地图本机插件

它显示了黑色屏幕时,试图加载地图在设备上的离子2谷歌地图本机插件
EN

Stack Overflow用户
提问于 2017-03-13 12:52:44
回答 4查看 22.8K关注 0票数 0

备注此项目必须在设备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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>

有人能帮我解决这个问题吗。我们对你的帮助深表感谢。

链接到文档

链接到项目回购

EN

回答 4

Stack Overflow用户

发布于 2017-03-13 13:42:58

代码语言:javascript
复制
let map = new MapPage();
  map.loadMap();`

MapPage是一个普通的类型记录类。它不是一个组件,它不会像这样运行生命周期挂钩:

代码语言:javascript
复制
 ngAfterViewInit() {
    this.loadMap();
  }

另外,由于元素位于map.loadMap()中,所以App.html中的调用home component无法工作。

我建议您将映射相关代码移动到home组件。还可以使用viewChild获取map div,这将是一个ElementRef对象。

家庭组件

代码语言:javascript
复制
  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
  }
}

角度文档参考:https://angular.io/docs/ts/latest/guide/

票数 0
EN

Stack Overflow用户

发布于 2017-05-05 13:16:01

在app.scss中,添加:

代码语言:javascript
复制
ion-app._gmaps_cdv_ .nav-decor{
   background: none !important;
}
票数 0
EN

Stack Overflow用户

发布于 2017-05-26 08:55:40

问题:在iOS平台上加载地图不起作用。它显示的是黑色屏幕。然而,地图请求在Google控制台上是正确更新的。

解决方案:我必须在.scss文件中添加以下代码-

代码语言:javascript
复制
ion-app._gmaps_cdv_ .nav-decor{
    display: none !important;
}

这是正确的工作和加载地图。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42764139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档