我正在尝试在传单地图上展示一些geoJSON数据。 geoJSON文件很大(60mb),并且在加载数据时网站的性能非常糟糕。 geoJSON有关流量密度等方面的内容,因此包含约23万个分段...
到目前为止,我已经尝试过通过创建here中提到的leaflet.vectorgrid
来实现leaflet.vectorgrid.d.ts
的实现。这是文件:
import * as L from "leaflet";
declare module "leaflet" {
namespace vectorGrid {
export function slicer(data: any, options?: any): any;
}
}
尽管性能仍然很差。
到目前为止,这是我的代码:
import { Component, OnInit } from "@angular/core";
import {
MapOptions,
LatLng,
TileLayer,
Map,
LeafletEvent,
Circle,
Polygon
} from "leaflet";
import * as L from "leaflet";
import { HttpClient } from "@angular/common/http";
@Component({
selector: "map-visualization",
templateUrl: "./map-visualization.component.html",
styleUrls: ["./map-visualization.component.scss"]
})
export class MapVisualizationComponent implements OnInit {
leafletOptions: MapOptions;
layersControl: any;
map: Map;
constructor(private http: HttpClient) {}
ngOnInit() {
this.initializeMap();
}
/**
* Initializes the map
*/
initializeMap() {
this.leafletOptions = {
layers: [
new TileLayer(
"https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}",
{
maxZoom: 18
}
)
],
zoom: 4,
center: new LatLng(48.1323827, 4.172899)
};
}
/**
* Once the map is ready, it pans to the user's current location and loads the map.geojson
* @param map Map instance
*/
onMapReady(map: Map) {
this.map = map;
if (navigator) {
navigator.geolocation.getCurrentPosition(position => {
this.map.setView(
new LatLng(position.coords.latitude, position.coords.longitude),
12
);
});
}
this.http.get("assets/map.json").subscribe((json: any) => {
L.geoJSON(json).addTo(this.map);
});
}
/**
* Return the current bound box
* @param event Leaflet event
*/
onMapMoveEnd(event: LeafletEvent) {
console.log("Current BBox", this.map.getBounds().toBBoxString());
}
}
最后,geoJSON总是那么大(60mb)... 因此,我想知道是否有一种方法可以过滤在当前边界框中获取的数据。
注意,该文件暂时存储在本地,但稍后我将从API提取该文件。
答案 0 :(得分:1)
以下方法应与传单一起使用(无需依赖其他库):
this.map.getBounds()
-返回LatLngBounds
-地图的边界(4个角的坐标)-“边界框”-您已经在做。
LatLngBounds
有一个名为contains()
的方法,如果true
的值在边界框内,则返回coords
:https://leafletjs.com/reference-1.5.0.html#latlngbounds-contains
您可以创建一个同时被onMapReady()
和onMapMoveEnd()
调用的方法,其功能如下:
addItemsToMap(items: []): Layer[] {
const tempLayer: Layer[] = [];
items.forEach(item => {
if (item.coordinates) {
const itemCoordinates = latLng(
item.coordinates.latitude,
item.coordinates.longitude
);
/** Only add items to map if they are within map bounds */
if (this.map.getBounds().contains(itemCoordinates)) {
tempLayer.push(
marker(itemCoordinates, {
icon: icon(
this.markers['red']
),
title: item.description
})
);
}
}
});
return tempLayer;
}
根据我的经验,Leaflet可以舒适地处理多达800项功能。如果用户体验允许,您还可以向用户显示一条消息,要求他们缩放或平移,直到要素数量低于允许的数量为止。
注意:contains()接受LatLng
和LatLngBounds
。要查看折线或多边形是否重叠或“包含在”边界框内,请执行以下操作之一:
LatLng
的形式传递。折线/多边形具有getCenter()
方法:https://leafletjs.com/reference-1.5.0.html#polyline-getcenter LatLngBounds
将折线“包装”到一个框中。折线/多边形具有getBounds()
方法:https://leafletjs.com/reference-1.5.0.html#polyline-getbounds 这两种方法显然将返回不同的结果:质心/重叠应返回更多匹配项。