Unity connects to Gaode map weather API

Unity accesses Amap API

  • 1. Unity uses Gaode to obtain weather
    • 1.0 Register a developer account
    • 1.1 The first step is to go to the AMAP developer platform address: https://lbs.amap.com/
    • 1.2 After logging in, create an application
    • 1.3 Then create the application
    • 1.4 After selecting the web service, there is
    • 1.5 key is the applied key that will be used later in the code.
    • 1.6 The following is to obtain the weather code
    • 1.7 The script can be hung anywhere to run the test
  • 2. Unity uses Gaode to obtain the current position
    • 2.1 The current position is printed as shown below
    • 2.2 It contains latitude and longitude, province, city code…
    • 2.3 The code to obtain the current location is as follows

1. Unity uses Gaode to obtain weather

1.0 Register a developer account

1.1 The first step is to go to the AMAP developer platform address: https://lbs.amap.com/

1.2 After logging in, create an application

1.3 Then create the application

1.4 It will be available after selecting the web service

1.5 key is the applied key that will be used later in the code

1.6 The following is to get the weather code

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class GetWeather : MonoBehaviour
{<!-- -->
    /// <summary>
    /// The key is obtained by creating an application application on the Amap Developer Platform
    /// </summary>
    private const string key = "key";
    

    private void Start()
    {<!-- -->
        Get("320100", GetDataType.Lives, data => {<!-- --> });
    }
 
    public enum GetDataType
    {<!-- -->
        /// <summary>
        /// Get live weather
        /// </summary>
        Lives,
        /// <summary>
        /// Get forecast weather
        /// </summary>
        Forecast
    }
    /// <summary>
    /// Get weather data
    /// </summary>
    /// <param name="city">City code</param>
    /// <param name="callback">Callback function</param>
    public void Get(string city, GetDataType type, Action<string> callback)
    {<!-- -->
        StartCoroutine(SendWebRequest(city, type, callback));
    }
    private IEnumerator SendWebRequest(string city, GetDataType type, Action<string> callback)
    {<!-- -->
        //url splicing
        string url = string.Format("https://restapi.amap.com/v3/weather/weatherInfo?key={0} & amp;city={1} & amp;extensions={2}", key , city, type == GetDataType.Lives ? "base" : "all");
        //Call API service using GET method
        using (UnityWebRequest request = UnityWebRequest.Get(url))
        {<!-- -->
            DateTime beginTime = DateTime.Now;
            yield return request.SendWebRequest();
            DateTime endTime = DateTime.Now;
            if (request.result == UnityWebRequest.Result.Success)
            {<!-- -->
                Debug.Log($"{<!-- -->beginTime} initiated a network request at {<!-- -->endTime} and received a response: \r\\
{<!-- -->request .downloadHandler.text}");
                callback.Invoke(request.downloadHandler.text);
            }
            else
            {<!-- -->
                Debug.Log($"Failed to initiate network request: {<!-- -->request.error}");
            }
        }
    }
}

The 1.7 script can be hung anywhere to run the test

2. Unity uses Gaode to obtain the current position

2.1 The current position is printed as shown below

2.2 It contains latitude and longitude, province, city code…

2.3 The code to obtain the current location is as follows

using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class GetUrl : MonoBehaviour
{<!-- -->
    public static string mykey = "your own key";
    private string urldingwei= "https://restapi.amap.com/v3/ip?key=7fc469b5bf1646dbee4ff55c7b07f35e";
    private string urlJingTaiDiTu = "https://restapi.amap.com/v3/staticmap?zoom=15 &size=500*500 &paths=10,0x0000ff,1,,:116.31604,39.96491;116.320816, 39.966606;116.321785,39.966827;116.32361,39.966957 &key=your own key";
    // Start is called before the first frame update
    void Start()
    {<!-- -->
        StartCoroutine(PostRequest_GET(urldingwei,data=> {<!-- --> }));
    }

    IEnumerator PostRequest_GET(string url, Action<string> callback)
    {<!-- -->
        using (UnityWebRequest webRequest = UnityWebRequest.Get(urll))
        {<!-- -->
            // send request
            yield return webRequest.SendWebRequest();

            // Check if there are any errors
            if (webRequest.result != UnityWebRequest.Result.Success)
            {<!-- -->
                Debug.LogError("Error: " + webRequest.error);
                Debug.Log(webRequest.responseCode);

            }
            else
            {<!-- -->
                // Request successful, process response data
                Debug.Log(webRequest.responseCode);
                Debug.Log("Response: " + webRequest.downloadHandler.text);
                //postDatas = webRequest.downloadHandler.text;
                callback.Invoke(webRequest.downloadHandler.text);
                Root root = JsonMapper.ToObject<Root>(webRequest.downloadHandler.text);
                //GetJsonData_HuiYiShi(root.data);
            }
        }
    }

    private WWW Loadab;
    public IEnumerator LoadWWW(string path)
    {<!-- -->
        Loadab = new WWW(path); //GloData.ABAdress=Application.streamingAssetsPath; The path can be modified according to your own needs
        yield return Loadab;
        if (Loadab.error != null)
        {<!-- -->
            Debug.Log(Loadab.error);
        }
        else
        {<!-- -->
            string oko = Loadab.text;
            Debug.Log(oko);
        }
    }

}
public enum output
{<!-- -->
    JSON,XML
}
public class Root
{<!-- -->
    /// <summary>
    ///
    /// </summary>
    public string status {<!-- --> get; set; }
    /// <summary>
    ///
    /// </summary>
    public string info {<!-- --> get; set; }
    /// <summary>
    ///
    /// </summary>
    public string infocode {<!-- --> get; set; }
    /// <summary>
    ///Jiangsu Province
    /// </summary>
    public string province {<!-- --> get; set; }
    /// <summary>
    /// Nanjing City
    /// </summary>
    public string city {<!-- --> get; set; }
    /// <summary>
    ///
    /// </summary>
    public string adcode {<!-- --> get; set; }
    /// <summary>
    ///
    /// </summary>
    public string rectangle {<!-- --> get; set; }
}



That’s all for this time, thanks for watching!!!