如何解决Angular 8 / Ionic 5 / Openlayers 6:无法在地图上居中
我想将OL Map定位在特征上。该功能由类属性引用。
当我单击用于触发居中的按钮时,实际上会调用以下方法,但地图不会移动。
我尝试使用Firefox和Brave,但没有成功。 该地图虽然在FF中移动了一次,但我修改了一些内容,无法使其再次正常工作。
这是我的代码(注释行也是我尝试过的行)
centerOnPosition() {
var point: Point = <Point>this.positionFeature.getGeometry();
console.log("Proj:",this.view.getProjection().getCode());
console.log("actual center",this.view.getCenter());
console.log("centering on pos",point.getCoordinates());
this.view.setCenter( point.getCoordinates());
// this.view.centerOn(point.getCoordinates(),this.map.getSize(),[500,500]);
// setTimeout(() => {
// this.map.updateSize();
// this.map.render();
// },1000);
// this.map.getView().animate({
// center: point.getCoordinates(),// duration: 1000
// })
// this.map.updateSize();
// this.view.setCenter( fromLonLat( point.getCoordinates(),get("EPSG:3857")));
// this.map.render();
}
这是输出:
Proj: EPSG:3857
actual center : Array [ 312807.60214986256,5350017.244833869 ]
centering on pos : [ 6153873.7653989205,-2394513.7334693754 ]
编辑:更多信息。
我怀疑坐标投影不好。我也想以地理定位为中心:
var geolocation = this.initGeolocation();
geolocation.setTracking(true);
initGeolocation() {
return new Geolocation({
trackingOptions: {
enableHighAccuracy: true,},projection: this.view.getProjection(),});
}
解决方法
因此,看来该问题是由组件构造函数中的一段代码引起的:如果用户未通过身份验证,则会通过某种路由将其转发到登录页面。 (我试图将此调用放在ngOnInit和ionViewWillEnter上没有成功)。
我还没有完全解决问题,我想第一次访问主页时的导航更改使地图构建混乱了……我很乐意接受一个能够提供更多见解的答案。
这是我现在必须注释的代码行:
if (!this.accountService.isAuthenticated()) {
console.log("not logged !!");
// this.router.navigateByUrl('/menu/login');
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。