【发布时间】: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