【发布时间】:2018-04-29 19:13:41
【问题描述】:
我必须在我的项目中添加一个带有谷歌地图的视图。我关注了official ionic native Google maps documentation,但它不起作用。
我为包含地图的 div 分配了背景颜色粉红色(用于测试),在将地图添加到 div 之前,div 正确显示。但是当地图被添加到 div 时,模态窗口的背景变为透明,并且 div 消失了。但没有显示错误。
我尝试了不同的方法将地图附加到div上,但结果都是一样的。
当我使用 Ionic 文档的代码时,我收到此错误消息“GoogleMaps [已弃用] 请使用 GoogleMaps.create()”。我尝试使用 GoogleMaps.create() 而不是构造函数的参数来避免此错误消息,但仍然无法正常工作。
我的离子含量:
<ion-content fullscreen overflow-scroll="true" class="container">
<div #map id="map"></div>
</ion-content>
部分css:
#map {
height: 90%;
width: 100%;
border-width: 5px;
border-color: red;
background: pink;
}
这里我们得到了 ts 的片段:
import {
GoogleMaps,
GoogleMap,
GoogleMapsEvent,
GoogleMapOptions,
CameraPosition,
MarkerOptions,
Marker
} from '@ionic-native/google-maps';
...
export class PerfilPage {
@ViewChild(Content) content: Content;
@ViewChild('map') mapElement: ElementRef;
map: GoogleMap;
...
ionViewDidLoad() {
// I use this for changing the nab-bar color
this.content.ionScroll.subscribe((scroll) => {
// console.log('user scrolled', scroll);
if ( scroll.scrollTop > 100 ) {
this.toolbar_color = "bg_search"
} else {
this.toolbar_color = "transparent"
}
this.ref.detectChanges();
});
// this.loadMap();
// this.initMap();
// this.testMap();
this.offiMap();
}
// Official Ionic documentation code
offiMap() {
let mapOptions: GoogleMapOptions = {
camera: {
target: {
lat: 43.0741904,
lng: -89.3809802
},
zoom: 18,
tilt: 30
}
};
this.map = this.googleMaps.create('map', mapOptions);
// Wait the MAP_READY before using any methods.
this.map.one(GoogleMapsEvent.MAP_READY)
.then(() => {
console.log('Map is ready!');
// Now you can use all methods safely.
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');
});
});
});
}
}
PS:API 密钥适用于其他项目(不是 Ionic 项目),所以我知道问题出在哪里。
谢谢:)
【问题讨论】:
-
你在设备或浏览器上测试过吗?
-
我正在设备上测试:)
标签: google-maps ionic-framework ionic3