當使用ol.source.Vector
的url
屬性的類加載通過XHR/AJAX爲您指定的網址:
設置這個選項指示源使用XHR加載器(見OL .featureloader.xhr)和ol.loadingstrategy.all,以便從該URL一次性下載所有功能。
您可以使用XHR/AJAX使用XMLHttpRequest
或像jquery這樣的具有XHR/AJAX功能的庫來自己加載文件。當您檢索GeoJSON集合時,您可以遍歷它所包含的特徵數組,並將其分解爲您所需的任何內容,並將這些特徵放入新的單獨的GeoJSON集合中。這裏是一個非常原始的例子給你的理念和思路:
假設以下GeoJSON的集合:
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
},
"properties": {
"name": "Free Willy"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [1, 1]
},
"properties": {
"name": "Moby Dick"
}
}, {
// Etc.
}]
}
這裏是如何(使用jQuery的$ .getJSON XHR功能)來加載它,並把它分裂分開收藏:
// Object to store separated collections
var whales = {};
// Load url and execute handler function
$.getJSON('collection.json', function (data) {
// Iterate the features of the collection
data.features.forEach(function (feature) {
// Check there is a whale already with that name
if (!whales.hasOwnProperty(feature.properties.name)) {
// No there isn't create empty collection
whales[feature.properties.name] = {
"type": "FeatureCollection",
"features": []
};
}
// Add the feature to the collection
whales[feature.properties.name].features.push(feature);
});
});
現在,您可以使用存儲在鯨魚對象中的單獨集合來創建圖層。注意,這不同於一些由使用url
屬性:
new ol.layer.Vector({
source: new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(whales['Free Willy'], {
featureProjection: 'EPSG:3857'
})
})
});
這裏的概念的工作示例:評論後http://plnkr.co/edit/rGwhI9vpu8ZYfAWvBZZr?p=preview
編輯:
如果你想爲威利所有座標:
// Empty array to store coordinates
var willysCoordinates = [];
// Iterate over Willy's features
whales['Free Willy'].features.forEach(function (feature) {
willysCoordinates.push(feature.geometry.coordinates);
});
現在willysCoordinates
包含座標的嵌套數組:[[0, 0],[2, 2]]
這解決了我的問題的後半部分,但我也需要將我的數據放入一個通用數組中,以便可以將座標輸入到我的動畫循環中。我認爲這是另一個問題,所以我會再發一個。 – interwebjill
根據你的意見編輯我的答案 – iH8
謝謝!現在我可以在我的geoJSON文件中使用多點功能,並使用它在圖層中逐段構建LineString。沒有必要的CSS動畫,這是很好的,因爲我有60個多點功能,每個功能大約800點。 – interwebjill