【发布时间】:2019-12-25 18:26:12
【问题描述】:
我有一个使用 Ionic 4 + Angular 8 的应用程序,我遇到了一个我不太了解的问题。
我的主页上有两个事件来获取我的位置,在我的 ngOnInit 上我有
ngOnInit() {
this.iniMap();
setTimeout(() => {
this.iniWatchingMap();
}, 2000);
}
我有两个函数,一个用于初始化地图,另一个用于在用户更改位置时启动订阅事件。
async iniMap() {
this.globalLoading = await this.loadingController.create({
message: 'Carregando Localização',
});
this.globalLoading.present();
this.globalMap = tt.map({
key: 'my-key',
container: 'map',
style: 'tomtom://vector/1/basic-main',
center: [-53.2, -10.3333333],
zoom: 16,
});
this.globalMap.addControl(new tt.FullscreenControl());
this.globalMap.addControl(new tt.NavigationControl());
this.globalMarker = new tt.Marker()
.setLngLat([-53.2, -10.3333333])
.addTo(this.globalMap);
this.globalMap.on('load', () => {
this.renderer.removeClass(this.mapElement, 'hide-map');
this.renderer.addClass(this.mapElement, 'visible');
this.mapLoaded = true;
this.globalLoading.dismiss();
});
}
另外一个是我的观看功能
watchPositionUser() {
const watchOptions = {
frequency : 3000,
timeout : 10000,
enableHighAccuracy: true // may cause errors if true
};
this.watchUser = this.geolocation.watchPosition(watchOptions);
this.watchUser.subscribe((data: any) => {
this.lat = data.coords.latitude;
this.long = data.coords.longitude;
// const points = [[-104.98485, 39.73845], [-118.24334, 34.05224], [-122.42017, 37.78008]];
this.globalMap.jumpTo({center: [this.long, this.lat]});
this.globalMarker
.setLngLat([this.long, this.lat])
.addTo(this.globalMap);
}, error => {
this.renderer.addClass(this.mapElement, 'hide-map');
this.mapError = true;
console.log('Error getting location', error);
this.globalLoading.dismiss();
});
}
当我不更改我的页面时这工作正常,但是当我移动到另一个页面后返回此页面时,我收到此错误
获取位置时出错 PositionError {code: 3, message: "Timeout expired"}
我知道那是超时,但如果我不设置超时,他需要几个小时,我不知道为什么。
另一件事我的位置非常错误它在离子中是正常的吗?还是因为我在网络上测试是错误的,但如果我使用手机它会是正确的?
这是我换页时出现新错误的地方
ngOnDestroy() {
this.watchUser.unsubscribe();
}
【问题讨论】:
-
1)。你试过
ionViewWillEnter()吗? 2)。是的,网络位置可能与移动设备有很大不同。 -
发生同样的事情,只有在第一次加载时才有效!
标签: javascript angular ionic-framework ionic4