Use this little class to iterate over the features of a GeoJSON
object and do stuff with them. Has a .forward()
and a .rewind()
method, just
like the good old cassette recorder.
Use it with e.g. events to iterate programmatically through the features in an endless loop.
Create the GeoJsonScroller as follows:
var scroller = new GeoJsonScroller(geoJsonDataObject, getDataFunction, sortFunction);
You can use notable methods on the class:
getData()
: retrieves the data for the current featureforward()
: move on to the next feature and get its data. If the end is reached, continue from startrewind()
: go back one feature and get its data. If the start is reached, continue with the last feature in the feature's array
You need to pass a function to retrieve data from a feature:
function getData (feature) {
var data = {},
cols = [ "title", "desc", "image", "icon", "color", "category",
"pitch", "bearing", "zoom", "speed", "curve" ];
data["center"] = feature.geometry.coordinates;
for (var key in feature.properties) {
if (cols.indexOf(key) > -1) { data[key] = feature.properties[key]; }
}
return data
}
scroller = new GeoJsonScroller(myGeoJson, getData);
var data = scroller.forward();
You can pass a function in order to sort the features by some criteria. Have a look at the docs for how to write such a function.
// this works for numerical props
function sortById (a, b) {
return a.properties.id - b.properties.id;
}
function getData (feature) {
// bla
}
scroller = new GeoJsonScroller(myGeoJson, getData, sortById);
var data = scroller.forward();
Have fun!