01-18-2023 20:41 - edited 01-18-2023 20:42
01-18-2023 20:41 - edited 01-18-2023 20:42
I'm new to sdk and I've built a clockface with weather. I used the fitbit weather api, and for reference I used the exact code of the "sdk-weather-clock" https://github.com/Fitbit/sdk-weather-clock
Everything worked so far, now I want to assign weather icons to weather conditioncodes. I already have icons in resources folder and want help assigning them.
Answered! Go to the Best Answer.
01-19-2023 00:35
01-19-2023 00:35
That said, I'd be inclined to extract the number and use it as an index into an associative array of image filenames.
Or, change the filenames to match the conditionCode values plus .png, then you wouldn't need a conditional test at all!
01-18-2023 21:05
01-18-2023 21:05
You can use the image element and set the href to the image name as it appears in the resources folder:
https://dev.fitbit.com/build/guides/user-interface/svg/#images
You can either create an element for each icon and show/hide them as needed in js, or use one image element and dynamically set the href:
let ele_weatherIcon = document.getElementById("weather-icon");
// ...
if (isSunny) {
ele_weatherIcon.href = "sunny-icon.jpg";
}
01-19-2023 00:10
01-19-2023 00:10
let ele_wimage = document.getElementById("wimage");
newfile.initialize(data => {
if (data.conditioncode == "32") {
ele_wimage.href = "windy.png";
}
if (data.conditioncode == "1") {
ele_wimage.href = "sunny.png";
}
});
not displaying any icons in sim
01-19-2023 00:14
01-19-2023 00:14
Use console.log to see what's getting executed, and the values of important variables (eg, data.conditioncode).
01-19-2023 00:27
01-19-2023 00:27
in simulator conditioncode shown as 32 (windy)
details.text = '${data.conditionCode}'
01-19-2023 00:32
01-19-2023 00:32
"32 (windy)" isn't equal to "32". See what damage you can do with this.
01-19-2023 00:35
01-19-2023 00:35
That said, I'd be inclined to extract the number and use it as an index into an associative array of image filenames.
Or, change the filenames to match the conditionCode values plus .png, then you wouldn't need a conditional test at all!
01-19-2023 00:46
01-19-2023 00:46
I typed 32 (windy) for reference purpose (ie., the conditionCode 32 refers weather condition windy)
actually its a type error.
what I should write is
if (data.conditionCode == "32")
instead I typed if (data.conditioncode == "32").
I feel stupid now.
Thanks everyone for your quick replies.
01-19-2023 01:39
01-19-2023 01:39
I totally agree with Gondwana to reference the href by renaming your images to the associated conditionCode.
So you just need one line for that.
image.href = …yourPath/data.conditionCode + '.png'
01-19-2023 02:54 - edited 01-19-2023 02:55
01-19-2023 02:54 - edited 01-19-2023 02:55
That's a good trick, but what if I need to assign two conditionCodes with only one image. I can duplicate the image with two different names, but it would increase the no of images.
01-19-2023 03:25 - edited 01-19-2023 03:26
01-19-2023 03:25 - edited 01-19-2023 03:26
I wouldn't bother having a copy of one or two images, otherwise you could make an exception for those groups of codes calling the same href
Or define elsewhere. In pseudo code:
code = code === this || that ? this : code
You could do so in an extra variable or directly with the href.
I never worked with the fitbit weather API so no idea how many overlaps of conditions exist there.
You might want to "group" them like above in an early state at the data itself, so you can use that for images, descriptive text...