javascript - 带有谷歌地图和 Ionic 3 的空白页
问题描述
Googlemaps 在浏览器和移动设备上的 Ionic 3 中显示一个白页。我已经正确安装了插件并导入了所有内容。测试时它提示我允许定位,然后显示一个没有 Google 徽标的白页。
我尝试重新安装所有插件并检查 API 密钥,一切似乎都有效。
.html
<ion-content padding>
<div #map id="map"></div>
</ion-content>
.module.ts
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { FindridePage } from './findride';
import { Geolocation } from '@ionic-native/geolocation';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { HttpClient } from '@angular/common/http';
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
@NgModule({
declarations: [
FindridePage,
],
imports: [
IonicPageModule.forChild(FindridePage),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: createTranslateLoader,
deps: [HttpClient]
}
})
] ,
exports: [
FindridePage
]
})
export class FindridePageModule {}
.ts
import { NavController,IonicPage,Platform } from 'ionic-angular';
import { GoogleMap,GoogleMaps } from '@ionic-native/google-maps';
import { Component, ViewChild, ElementRef } from '@angular/core';
import { Geolocation } from '@ionic-native/geolocation';
declare var google: any;
@IonicPage()
@Component({
selector: 'page-findride',
templateUrl: 'findride.html'
})
export class FindridePage {
@ViewChild('map') mapElement: ElementRef;
map: any;
markers = [];
constructor(public navCtrl: NavController, private _googleMaps: GoogleMaps,public platform: Platform,private geolocation: Geolocation) {
setTimeout(this.initMap(), 10000);
}
initMap() {
console.log('starting');
this.geolocation.getCurrentPosition({ maximumAge: 3000, timeout: 5000, enableHighAccuracy: true }).then((resp) => {
let mylocation = new google.maps.LatLng(resp.coords.latitude,resp.coords.longitude);
this.map = new google.maps.Map(this.mapElement.nativeElement, {
zoom: 15,
center: mylocation
});
});
let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
this.deleteMarkers();
let updatelocation = new google.maps.LatLng(data.coords.latitude,data.coords.longitude);
let image = 'assets/imgs/blue-bike.png';
this.addMarker(updatelocation,image);
this.setMapOnAll(this.map);
});
}
addMarker(location, image) {
let marker = new google.maps.Marker({
position: location,
map: this.map,
icon: image
});
this.markers.push(marker);
}
setMapOnAll(map) {
for (var i = 0; i < this.markers.length; i++) {
this.markers[i].setMap(map);
}
}
clearMarkers() {
this.setMapOnAll(null);
}
deleteMarkers() {
this.clearMarkers();
this.markers = [];
}
listride(){
this.navCtrl.push('ListridePage');
}
}
解决方案
如下设置地图元素的高度和宽度,您将看到地图。如果已加载。
<div #map id="map" style="height:500px; width:300px;">
推荐阅读
- tcl - 如何获取 tcl 文件?
- google-app-engine - 如何在 Google App Engine 中使用 Ruby 2.5 部署 Rails 5
- python - Selenium Python get_element by ID 失败
- rust - 如何在不破坏借用规则的情况下包装可窥视的迭代器
- php - 为什么我的 textarea 中提交的文本呈现为单行?
- php - 在数组 PHP 中添加键
- c++ - 将变量从主窗口传递到另一个 qt c++ 表单
- javascript - 输入长度未定义
- .htaccess - 使用条件重定向 301
- python - DataFrame max() 不返回最大值