Get elevation by the coordinate

Back
Tags:
Google API
Elevation
Web services
Example of get elevation value in the coordinate using Google Elevation API.
Usage instructions:
Add this script to map GameObject and start the scene.
Click on the map and in console will be shown elevation value.
GetElevationExample.cs
/*         INFINITY CODE         */
/*   https://infinity-code.com   */

using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    /// <summary>
    /// Example of get elevation value in the coordinate using Google Elevation API.
    /// </summary>
    [AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/GetElevationExample")]
    public class GetElevationExample : MonoBehaviour
    {
        /// <summary>
        /// Reference to the map. If not specified, the current instance will be used.
        /// </summary>
        public OnlineMaps map;
        
        private void Start()
        {
            // If the map is not specified, get the current instance.
            if (map == null) map = OnlineMaps.instance;
            
            // Subscribe to click on map event.
            map.control.OnMapClick += OnMapClick;
        }

        private void OnMapClick()
        {
            // Get the coordinates where the user clicked.
            Vector2 coords = map.control.GetCoords();

            // Get elevation on click point
            OnlineMapsGoogleElevation.Find(coords).OnComplete += OnComplete;
        }

        private void OnComplete(string response)
        {
            // Get results from response string
            OnlineMapsGoogleElevationResult[] results = OnlineMapsGoogleElevation.GetResults(response);

            if (results == null)
            {
                // If results is null log message
                Debug.Log("Null result");
            }
            else
            {
                // Shows first result elevation
                Debug.Log(results[0].elevation);
            }
        }
    }
}