【发布时间】:2018-05-27 22:13:12
【问题描述】:
我正在使用 Leaflet 将 Ionic App 与 OSM 集成。我在 page.html 文件中有 ,其余代码在我的 page.ts 文件中。所以,我没有使用 @ViewChild() 来找到那个 div。因为,Leaflet 的 L.map('map') 应该可以找到它。我正在 ionViewDidLoad 上初始化我的地图,并在 ionViewDidEnter 中进行繁重的工作,例如将从 post 请求中检索到的 osm 数据转换为 geoJSON 数据,然后在地图上放置很多标记。我有点试图释放 ionViewDidLeave 中的资源,使 this.map = null;。这是一个正确的实现吗?谁能在实施方面提出建议。因为当我滚动时页面有点冻结。
ionViewDidLoad() {
this.map = L.map('map').setView([41.0131, 28.9641], 18);
L.tileLayer('https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(this.map);
}
ionViewDidEnter() {
this.filterProvider.getData(resource).subscribe(data => {
if (this.map !== null) {
L.geoJSON(osmtogeojson(data), {
style: function(feature) {
return {
color: "#0288D1"
};
},
pointToLayer: function(feature, latlng) {
return L.marker(latlng, {
icon: L.icon({
iconUrl: 'assets/map/' + resource.toLowerCase() +'.png'
})
});
},
onEachFeature: function(feature, layer) {
layer.bindPopup(JSON.stringify(feature.properties));
}
}).addTo(this.map);
}
});
}
ionViewDidLeave() {
this.map = null;
this.selectedResource = null;
}
【问题讨论】:
-
“在地图上放置很多标记”通常是多少?这可能是您的页面冻结的原因。
标签: leaflet ionic3 openstreetmap