如何仅将非常大的 geojson 数组中的前 50 个列表项添加到侧边栏而不是全部

How to only add first 50 list items from a very large geojson array to sidebar instead of all

我有一个非常大的地点 geojson 文件(目前有 1000 个),我使用 turf.js 对它们进行排序,以便找到它们与地图上某个点的距离(由地理编码器触发) .这是有效的,但是我打算将按距离排序的地点的结果添加到侧边栏,并且由于大量的地点(大量计算)需要很长时间才能加载。这是当前代码:

[places = 包含所有信息的源 geojson 文件]

        geocoder.on('result', function(ev) {
    
                                        var searchResult = ev.result.geometry;
    
                                        var options = { units: 'miles' };
    
                                        //GIVES EACH PLACE A DISTANCE VALUE
                                        places.features.forEach(function(place) {
                                            Object.defineProperty(place.properties, 'distance', {
                                                value: turf.distance(searchResult, place.geometry, options),
                                                writable: true,
                                                enumerable: true,
                                                configurable: true
                                            });
                                        });
    
                                        //GIVING EACH PLACE A VALUE IN ORDER OF CLOSEST TO FURTHEST
                                        places.features.sort(function(a, b) {
                                            if (a.properties.distance > b.properties.distance) {
                                                return 1;
                                            }
                                            if (a.properties.distance < b.properties.distance) {
                                                return -1;
                                            }
                                            return 0; // a must be equal to b
                                        });
    
    
                                        var listings = document.getElementById('listings');
                                        while (listings.firstChild) {
                                            listings.removeChild(listings.firstChild);
                                        }
(the functions below go here)

(尝试创建一组新的较小的位置以放入下面的函数 - 而不是使用整个 'places' 文件)

//THIS FUNCTION ADDS THE SORTED PLACES TO THE SIDEBAR
                                        buildLocationList(places);
                                        createPopUp(places.features[0]);
                                        }); 

所以基本上我试图创建一个新的地点列表,该列表根据地理编码器的结果在本地存储,它比整个 'places' geojson 文件小得多(可能包含 50 个最近的地点而不是每一个)。由此生成一个函数,我可以将其传递给 buildLocationList 函数,该函数将它们添加到侧边栏。

如有任何帮助,我们将不胜感激 (:

尝试添加 localStorage.setItem('TopFiftyPlaces', JSON.stringify(places.slice(49))); 排序后。

这行代码取地方的前五十个元素,然后存入本地存储。

要检索存储的数组,您可以这样做:

const topFiftyPlaces = JSON.parse(localStorage.getItem('topFiftyPlaces'));

然后您可以将 topFiftyPlaces 变量而不是 places 传递给 buildLocationList,如下所示: buildLocationList(topFiftyPlaces);