我正在研究Ionic 3项目,并且为了实现与地图相关的功能,我选择了Ionic Team建议的Google Maps插件。 Google Maps Ionic插件基本上是cordova-plugin-googlemaps的包装。我已经遵循了presentation中提到的步骤。
我无法在ID为map_canvas
的元素中显示地图,该元素位于页面html之一中。到目前为止,这是我尝试过的:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { GoogleMaps, GoogleMap, Marker } from '@ionic-native/google-maps';
@IonicPage()
@Component({
selector: 'page-step-two',
templateUrl: 'step-two.html',
})
export class StepTwoPage {
private stepTwoForm: FormGroup;
private map: GoogleMap;
constructor(private formBuilder: FormBuilder) {
this.stepTwoForm = this.formBuilder.group({
prename: ['', Validators.required],
name: ['', Validators.required],
email: ['', Validators.required],
mobile: ['', Validators.required],
yearLiving: ['', Validators.required],
});
}
ionViewDidLoad() {
// Create a map after the view is ready
// and the native platform is ready.
this.loadMap();
}
loadMap() {
this.map = GoogleMaps.create('map_canvas'); // <--- Problematic statement
}
goToStepThree() {
}
}
这是html页:
<ion-header>
<ion-navbar>
<ion-title>Step - 2</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<progress-bar [stepCount]="5" [currentStep]="2"></progress-bar>
<ion-card class="step-body-card">
<ion-card-header>
<h1 text-center class="card-title">Persona</h1>
</ion-card-header>
<ion-card-content>
<form [formGroup]="stepTwoForm" (ngSubmit)="goToStepThree()">
<ion-grid>
<ion-row>
<ion-col col-12 col-sm>
<ion-item>
<ion-input type="text" placeholder="Prename" formControlName="prename"></ion-input>
</ion-item>
</ion-col>
<ion-col col-12 col-sm>
<ion-item>
<ion-input type="text" placeholder="Name" formControlName="name"></ion-input>
</ion-item>
</ion-col>
</ion-row>
<ion-row>
<ion-col col-12 col-sm>
<ion-item>
<ion-input type="email" placeholder="Email" formControlName="email"></ion-input>
</ion-item>
</ion-col>
<ion-col col-12 col-sm>
<ion-item>
<ion-input type="tel" placeholder="Mobile" formControlName="mobile"></ion-input>
</ion-item>
</ion-col>
</ion-row>
<ion-row>
<ion-col col-12 col-sm>
<ion-item>
<ion-input type="number" min="0" placeholder="Year of living" formControlName="yearLiving"></ion-input>
</ion-item>
</ion-col>
<ion-col col-12 col-sm>
</ion-col>
</ion-row>
</ion-grid>
<button margin-top ion-button type="submit" [disabled]="!stepTwoForm.valid">Continue</button>
</form>
</ion-card-content>
</ion-card>
</ion-content>
我尝试在Android上运行,但它给出了一个错误,没有在map_canvas
元素上显示任何内容。该错误是由cordova产生的:
Error in Success callbackId: CordovaGoogleMaps1188158820 : ReferenceError: element is not defined
这是控制台的堆栈跟踪:
Uncaught ReferenceError: element is not defined
at Map.<anonymous> (:8100/plugins/cordova-plugin-googlemaps/www/CordovaGoogleMaps.js:645)
at Map.trigger (:8100/plugins/cordova-plugin-googlemaps/www/BaseClass.js:68)
at Map.set (:8100/plugins/cordova-plugin-googlemaps/www/BaseClass.js:35)
at Map.getMap (:8100/plugins/cordova-plugin-googlemaps/www/Map.js:152)
at :8100/plugins/cordova-plugin-googlemaps/www/CordovaGoogleMaps.js:881
at Object.callbackFromNative (cordova.js:291)
at <anonymous>:1:9
我可以确认此错误是在初始化或创建映射时由语句产生的,在page.ts中指出为问题语句。我无法获得此错误的根本问题。
请帮助我对问题进行故障排除并提出修复建议。
编辑
调试后,我发现错误仅在map_canvas
元素(div)位于form
内时发生,如果我从form
标记中取出,它会按预期工作并映射节目。
FormBuilder
可能是一个错误或出了点问题。我需要在表单中显示Google Map,因此请帮助我修复Google Map插件的这种奇怪行为。已编辑的问题,包括完整的表格供参考,并简化了地图创建逻辑。