我正在研究离子应用。
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { MenuController, NavController, NavParams } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
import { RoleService } from '../../services/role.service';
import { RoleViewDto } from '../../contracts/role/roleViewDto';
import { RoleType } from '../../contracts/enums/enums';
import { GoogleMapsService } from '../../services/google-maps.service';
import { CommonService } from '../../services/common.service';
declare var google;
@Component({
selector: 'pwssr-welcome',
templateUrl: 'welcome.page.html',
})
export class WelcomePage implements OnInit {
@ViewChild('map') mapElement: ElementRef;
map: any;
role: RoleViewDto;
roleType = RoleType;
lat: number;
lng: number;
marker: any;
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public geolocation: Geolocation,
private roleService: RoleService,
private googleMapsService: GoogleMapsService,
private commonService: CommonService,
) {
}
ngOnInit() {
this.commonService.setTitle("Sweet Rides");
this.roleService.getRole().subscribe((res) => {
this.role = res;
});
}
ionViewDidLoad() {
this.loadMap();
}
loadMap() {
this.geolocation.getCurrentPosition().then((position) => {
let latLng = new google.maps.LatLng(-32.3003176, 115.7749078);
this.map = this.googleMapsService.get(latLng, this.mapElement);
}, (err) => {
console.log(err);
}).catch(error => {
console.log('[DEBUG] Promise Unfulfilled. ' + JSON.stringify(error));
});
}
此代码正在使用Web浏览器,但在构建apk后它无法在Android设备上运行。我使用本教程https://ionicframework.com/docs/native/geolocation/
安装了Cordova和Ionic Native插件ionic info
cli packages: (C:\Users\Peda\AppData\Roaming\npm\node_modules)
@ionic/cli-utils : 1.18.0
ionic (Ionic CLI) : 3.18.0
global packages:
cordova (Cordova CLI) : not installed
local packages:
@ionic/app-scripts : 1.3.7
Cordova Platforms : android 6.2.3
Ionic Framework : ionic-angular 3.6.0
System:
Node : v6.11.2
npm : 3.10.10
OS : Windows 10
Environment Variables:
ANDROID_HOME : not set
Misc:
backend : legacy
看起来this.geolocation.getCurrentPosition不起作用。此外,我在控制台
中没有任何错误消息答案 0 :(得分:0)
试试这个让我知道:
let element = document.getElementById('map');
let map: GoogleMap = this.googleMaps.create(element);
map.getMyLocation((loc: any)=>{
console.log("--Latitude--" + loc.latLng.lat);
console.log("--Longitude--" + loc.latLng.lng);
}).catch((error) => {
console.log("--Error--");
});