首页 > 解决方案 > 如何在离子 3 中的 leaflet.map("map").setView 中使用当前纬度

问题描述

this.map = leaflet.map("map").setView([this.latitude, this.longitude], 10);

还使用此代码获取当前纬度

this.geolocation.getCurrentPosition().then( pos =>{ 

this.latitude = pos.coords.latitude;
this.longitude = pos.coords.longitude;

}).catch(err => console.log(err));

但它不会在真实设备中第一次渲染。

标签: ionic3

解决方案


你在打电话吗

this.map = leaflet.map("map").setView([this.latitude, this.longitude], 10);

在地理定位解决设备坐标之前?

设备可能需要一些时间来解决其位置。如果您设置视图并过早渲染地图,那么这可能解释了为什么您在第一次加载时遇到问题。

在 .then 方法中设置传单地图的视图可以解决您的问题。但是,您可能需要处理较长的解析时间,尤其是当地图是您的应用程序的关键组件时。IE,您不希望您的用户在加载地图时查看空白屏幕。


推荐阅读