Skip to content

Instantly share code, notes, and snippets.

@newyellow
Created August 14, 2020 14:52
Show Gist options
  • Save newyellow/f1b633dd71658a7242c19f426ee29a04 to your computer and use it in GitHub Desktop.
Save newyellow/f1b633dd71658a7242c19f426ee29a04 to your computer and use it in GitHub Desktop.
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div>
<input type="button" value="print" onmousedown="printArray();" />
<input type="button" value="print 復興區" onmousedown="printTargetTemp('復興區');" />
<div id="buttonsArea">
</div>
</div>
<script>
let namesArray = [];
let tempData = [];
$(window).on('load', function (){
getWeatherData();
});
function getWeatherData () {
let apiUrl = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/F-D0047-007?Authorization=CWB-15391347-353E-466D-8C41-3C1D7D3CF28B&format=JSON";
$.get(apiUrl, handleWeatherData);
}
function handleWeatherData (data){
let namesCount = data.records.locations[0].location.length;
// put name
for(let i=0; i< namesCount; i++)
{
let currentLocationName = data.records.locations[0].location[i].locationName;
namesArray[i] = currentLocationName;
let newButton = $('<div>');
newButton.html(currentLocationName);
newButton.attr('id', i);
newButton.on('click', function(){
printTargetTemp(this.id);
});
$('#buttonsArea').append(newButton);
}
for(let i=0; i< namesCount; i++)
{
let currentLocation = data.records.locations[0].location[i];
let dataCount = currentLocation.weatherElement[1].time.length;
let currentName = currentLocation.locationName;
console.log(currentLocation.locationName);
tempData[i] = [];
for(let t=0; t< dataCount; t++)
{
let targetValue = currentLocation.weatherElement[1].time[t].elementValue[0].value;
tempData[i][t] = targetValue;
}
}
}
function printArray () {
console.log("array print:");
console.log(tempData);
}
function printTargetTemp (targetLocationIndex)
{
//console.log(targetLocationIndex);
console.log(tempData[targetLocationIndex]);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment