【问题标题】:ionic 3 white screen Google map离子3白屏谷歌地图
【发布时间】:2018-07-17 01:41:39
【问题描述】:

我试图在我的 ionic 3 项目中显示谷歌地图。但是当我输入

离子服务

它只在控制台中显示地图应该没有错误的灰色屏幕。

这是我的代码:

home.ts*

import { Component , ViewChild,ElementRef } from '@angular/core';
import { NavController , Platform } from 'ionic-angular';
declare var google: any;
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  @ViewChild("map") mapRef :ElementRef ;
  constructor(public navCtrl: NavController, public platform :Platform) {
    platform.ready().then(() => {
      this.showMap();
    });   
  }
  showMap(){
    const location = new google.maps.LatLng(30.414047, -9.580157);

    const option ={
      center :location ,
      zoom : 15  ,
     streetViewControl : false ,
     mapTypeId :'roadMap'
    } ;
  const map =new google.maps.Map(this.mapRef.nativeElement ,option ) ;
this.addMarker(location, map);

  }
  addMarker(position , map){
return new google.maps.Marker({
  position ,
  map
}) ;
  }}

home.html

<ion-header>
  <ion-navbar color="primary">
    <ion-title>
      Google maps 
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <div #map id="map"></div>
</ion-content>

home.sccs

page-home {
#map {
        height: 100%;
    }
}

加上我添加了这个:

<script src="http://maps.google.com/maps/api/js?key=AIzaSyCIJTDtdHB4s381xQEwzVsfdBxbr9kWkJk" async defer></script>

*更新:* 当我评论这一行时:

 showMap(){
    // streetViewControl : false ,
     //mapTypeId :'roadMap'
    } ;

地图出现了。我不知道为什么。

【问题讨论】:

  • 你在哪里添加了谷歌地图脚本标签?
  • @JorgeMejia 在我的 index.html 中

标签: google-maps ionic-framework ionic3


【解决方案1】:

问题可能在于您在constructor 中调用它。尝试将构造函数代码放在 ngAfterViewInit() 方法中。

【讨论】:

  • 还是同样的问题。这是我的页面的外观:ibb.co/b51BrH
【解决方案2】:

我对标志性框架了解不多。
但是您在 google maps API 上的脚本没有回调,它会异步加载。
为什么不尝试在调用脚本时加入回调。

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=showMap" async defer></script>

【讨论】:

  • 非常感谢!我加了。
【解决方案3】:

这里是 plnkr 的一个例子,你如何初始化你的谷歌地图。

首先你必须在 ionViewDidLoad() 中初始化你的地图,你的地图容器也需要有一个宽度:100% 和高度:100%;

ionViewDidLoad() {
    this.initMap();
  }

initMap(){
    //const location = new google.maps.LatLng(20.623736, -87.073395);
    let myLatLng = {lat: 20.623736, lng: -87.073395};
    console.log(location);
    const option ={
         center :myLatLng ,
         zoom : 15  ,
         //streetViewControl : false ,
         //mapTypeId :'roadMap'
    } ;
    //console.log(this.mapElement)
    const map = new google.maps.Map(this.mapElement.nativeElement ,option ) ;
    this.addMarker(myLatLng, map);
  }

这是因为您没有使用本机插件。您需要等待页面加载完成。

另一个观察是在您的函数 addMarker 中,您只传递必须放置键 val 参数的位置和地图值。

addMarker(position , map){
     return new google.maps.Marker({
        position: position,
        map: map,
        title: 'Hello World!'
      });
   }

这是我为您制作的完整示例。

https://plnkr.co/edit/pQqJoJrqfUVfAgiv1HA9?p=preview

【讨论】:

  • 非常感谢先生!我真的很感谢你为帮助我而付出的努力。它现在很糟糕:)
  • 如果你想要更好的地图体验我推荐你使用原生插件,有了这个插件你可以拥有所有的触摸手势:) ionicframework.com/docs/native/google-maps
  • 谢谢先生!我会的。
猜你喜欢
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
相关资源
最近更新 更多