· 6 years ago · Nov 23, 2019, 04:30 AM
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEngine.Networking;
5
6public class WeatherAPI : MonoBehaviour
7{
8
9 public string icone;
10 public string main;
11
12 private string lat = "35";
13 private string lon = "139";
14
15 //OpenWeatherMap
16 private string apiKeyOpen = "73fe20ef57b05534cc5d787d6a8959e7";
17 private string urlOpen = "http://api.openweathermap.org/data/2.5/weather?lat=";
18 //API Key: 73fe20ef57b05534cc5d787d6a8959e7
19
20 //AccuWeather
21 //private string urlAccuLocationKey = "http://dataservice.accuweather.com/locations/v1/cities/geoposition/search";
22 //private string urlAccuData = "http://dataservice.accuweather.com/currentconditions/v1/"; //+LocationKey
23 //API Key: M82GQGOA4jBYYH1TWj9TYiImGHb45rby
24
25 //Windy
26 //private string urlWindy = "";
27 //API Key: KSvkoEDstPlpZDTtixCCHjw3mGTqfCy5
28
29 // Start is called before the first frame update
30 IEnumerator Start()
31 {
32 urlOpen = urlOpen + lat + "&lon=" + lon + "&APPID=" + apiKeyOpen;
33
34 UnityWebRequest request = UnityWebRequest.Get(urlOpen);
35 yield return request.SendWebRequest();
36 if (request.error == null || request.error == "")
37 {
38 string jsonString = request.downloadHandler.text;
39 //Debug.Log("Resposta: " + jsonString);
40 WeatherRaiz weather = setWeatherAttributes(jsonString);
41 icone = weather.weather[0].icon;
42 main = weather.weather[0].main;
43 Debug.Log(icone);
44
45 }
46 else
47 {
48 Debug.Log("Error: " + request.error);
49 //Outra API?
50 }
51 }
52
53 private WeatherRaiz setWeatherAttributes(string jsonString)
54 {
55 WeatherRaiz weatherJson = JsonUtility.FromJson<WeatherRaiz>(jsonString);
56 //Debug.Log(weatherJson.coord.lat);
57 //Debug.Log(weatherJson.weather[0].icon);
58 return weatherJson;
59 }
60
61 public string weatherIcon()
62 {
63 return icone;
64 }
65
66 public string weatherMain()
67 {
68 return main;
69 }
70}