我试图在传单地图上展示一些geoJSON数据。geoJSON文件很大(60 is ),当数据被加载时,站点的性能非常糟糕。geoJSON是关于交通密度和更多,所以它包含约230 K段.
到目前为止,我尝试的是通过创建leaflet.vectorgrid中提到的leaflet.vectorgrid.d.ts来实现here。这是一个文件:
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总是那么大(60 is ).因此,我想知道是否有一种方法来过滤在当前边界框中获取的数据。
注意到文件目前存储在本地,但稍后我将从API中获取它。
发布于 2019-10-31 03:00:53
以下方法应在传单上(不依赖于另一个库)进行本机工作:
this.map.getBounds() -返回LatLngBounds --地图的边界(4个角的坐标)--“包围框”--您已经在这样做了。
LatLngBounds有一个名为contains()的方法,如果coords的值在边框中:https://leafletjs.com/reference-1.5.0.html#latlngbounds-contains,则返回true。
您可以创建一个将从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;
}根据我的经验,传单可以舒舒服服地处理800多个特征。如果用户体验允许,您也可以向用户显示一条消息,要求他们缩放或平移,直到功能的数量低于可容忍的数目为止。
注意: contains()接受LatLng和LatLngBounds。若要查看多边形或多边形是否与边框重叠或“包含”,请执行以下操作:
LatLng传递。多边形/多边形有一个https://leafletjs.com/reference-1.5.0.html#polyline-getcenterLatLngBounds传递到一个框中。多边形/多边形有一个https://leafletjs.com/reference-1.5.0.html#polyline-getbounds方法:getBounds()方法
这两种方法将明显地返回不同的结果:质心/重叠应该返回更多匹配。
https://stackoverflow.com/questions/58376872
复制相似问题