Waldy
January 19, 2014, 5:26pm
1
I need help getting my standalone application to detect what the weather is. I thought of 2 theory’s which i could do, but i don’t know how to do it.
Is there a Weather.Now function? Just like TheTime.Now.Day; but for weather?
If there isn’t a .Now statement for weather is there a way which i could use WWW to gather a string of information from a website e.g. I use the website Google but i want to get the temperature information from it, is that possible?
Thanks
Yes, you can use something like that:
Simple and fast and free weather API from OpenWeatherMap you have access to current weather data, hourly, 5- and 16-day forecasts. Historical weather data for 40 years back for any coordinate. Weather maps, UV Index, air pollution
and historical...
or any other weather data provider…
then use Unity - Scripting API: WWW to get the data and parse it, so you can use it.
Fixxed it @Waldy
Blockquote
using UnityEngine;
using System.Collecstrong texttions;
using System.Xml;
using System;
public class ReadXml : MonoBehaviour {
IEnumerator Start()
{
string url = "http://api.openweathermap.org/data/2.5/find?q=London&type=accurate&mode=xml&lang=nl&units=metric&appid=bd82977b86bf27fb59a04b61b657fb6f";
WWW www = new WWW(url);
yield return www;
if (www.error == null){
Debug.Log("Loaded following XML " + www.data);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(www.data);
Debug.Log("City: " + xmlDoc.SelectSingleNode("cities/list/item/city/@name").InnerText);
Debug.Log("Temperature: " + xmlDoc.SelectSingleNode("cities/list/item/temperature/@value").InnerText);
Debug.Log("humidity: " + xmlDoc.SelectSingleNode("cities/list/item/humidity /@value").InnerText);
Debug.Log("Cloud : " + xmlDoc.SelectSingleNode("cities/list/item/clouds/@value").InnerText);
Debug.Log("Title: " + xmlDoc.SelectSingleNode("cities /list/item/weather/@value").InnerText);
}
else
{
Debug.Log("ERROR: " + www.error);
}
}
}