您目前通過兩個jQuery的$.getJSON
從mapbox帳戶加載數據和直接。
那麼如果我的理解是正確的,你想通過使用leaflet-omnivore插件替換這些方法?
直接更換是相當簡單的,因爲你可以直接使用:
var geojsonLayer = omnivore.geojson("filePath", null, L.mapbox.featureLayer());
geojsonLayer.addTo(map);
現在,當你想你的羣集標記(使用Leaflet.markercluster插件在你的情況)變得稍微複雜一些。但它與$.getJSON
類似,因爲兩者都執行異步AJAX請求,您必須在回調中進行轉換。
隨着瓣葉雜食動物,可以使用.on("ready", callback)
語法:
var geojsonLayer = omnivore.geojson("filePath", null, L.mapbox.featureLayer())
.on("ready", function() {
var markers = L.markerClusterGroup();
markers.addLayer(geojsonLayer);
markers.addTo(mymap);
});
更新的jsfiddle:https://jsfiddle.net/1uuubmwb/39/
編輯
好,我錯過了你的一部分,你「想搜索數據「,就像你指向的mapbox中的教程所做的那樣。
就你而言,由於你想要對標記進行聚類,所以你不需要直接使用mapbox要素圖層,而是使用標記羣集組。
一種解決方法是更換羣集組每次你改變你的geojsonLayer
mapbox功能層的過濾條件的內容:
// this will "hide" markers that do not match the filter.
geojsonLayer.setFilter(showCode);
// replace the content of marker cluster group.
markers.clearLayers();
markers.addLayer(geojsonLayer);
然後爲你彈出,你必須創建並手動將它綁定作爲mapbox功能層需要您GeoJSON的數據使用title
屬性(如果是的話,它會自動使用該信息來填充彈出/「提示」的內容):
function attachPopups() {
// Create popups.
geojsonLayer.eachLayer(function (layer) {
var props = layer.feature.properties;
layer.bindPopup(
"<b>Code unité :</b> " + props.CODE_UNITE + "<br />" +
"<b>Adresse web :</b> <a href='" + props.ADRESSE_WEB + "' target='_blank'>" + props.ADRESSE_WEB + "</a>"
);
});
}
不幸的是,它看起來像.setFilter()
刪除那個彈出窗口,所以你需要在每個setFilter
之後調用這個attachPopups()
函數。
演示:https://jsfiddle.net/1uuubmwb/40/
感謝您在JSFiddle中分享您的代碼,它確實有助於提供支持。請注意,在SO中,第一步是查明您的問題以減少代碼並將其分享到問題主體中,製作一個「[Minimal,Complete,and Verifiable example](https://stackoverflow.com/help/mcve) )」。有關[請求幫助](https://stackoverflow.com/help/asking)的更多詳細信息。 JSFiddle是額外的獎勵。 – ghybs