Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var map = L.map('map'),
realtime = L.realtime(getCustomData, {
interval: 3 * 1000
}).addTo(map);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
realtime.on('update', function() {
map.fitBounds(realtime.getBounds(), {maxZoom: 13});
console.log(arguments);
});
function getCustomData(success, error) {
let url = "stream.json"; //url of service
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function () {
if (xhr.status === 200) {
var res = convertToGeoJSON(xhr.responseText);
success(res);
} else {
var e = new Error("HTTP Rquest")
error(e, xhr.status);
}
};
xhr.send();
function convertToGeoJSON(input) {
//convert input to Object, if it is of type string
if (typeof(input) == "string") {
input = JSON.parse(input);
}
var fs = {
"type": "FeatureCollection",
"features": []
};
for (var i = 0; i < input.length; i++) {
var ele = input[i];
var feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ele['longitude'], ele['latitude']]
}
};
feature.properties = ele;
//set the id
feature.properties["id"]=i;
//check that the elements are numeric and only then insert
if (isNumeric(ele['longitude']) && isNumeric(ele['latitude'])) {
//add this feature to the features array
fs.features.push(feature)
}
}
//return the GeoJSON FeatureCollection
return fs;
}
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.