Trying to create a GeoJSON list using data from an API. Extracted location details like name, latitude, and longitude from the API and stored them in arrays. However, when trying to populate the GeoJSON geometry coordinates with the array values, the console is showing them as undefined. How can I successfully transfer the API values into the GeoJSON format?
https://i.sstatic.net/nPEQp.png
locname = [];
loclat = [];
loclong = [];
var api = $.getJSON("https://api.data.gov.sg/v1/environment/rainfall?date=2019-06-21",
function rainfall(data_rainfall){
console.log(data_rainfall);
var j;
for (j in data_rainfall.metadata.stations){
locname[j] = data_rainfall.metadata.stations[j].name;
loclat[j] = data_rainfall.metadata.stations[j].location.latitude;
loclong[j] = data_rainfall.metadata.stations[j].location.longitude;
}
locname.push(locname[j]);
loclat.push(loclat[j]);
loclong.push(loclong[j]);
return locname, loclat, loclong;
});
console.log(locname); // Values appear in console
console.log(loclat); // values appear in console
console.log(loclong); // values appear in console
var apiGeo = {
type: "FeatureCollection",
features: []
};
for (i in api){
apiGeo.features.push({
"type":"Feature",
"geometry":{
"type": "Point",
"coordinates": [loclong[i], loclat[i]]
}
});
}
console.log(apiGeo); // values appear as 'undefined' in console
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>