Search code examples
azuredictionary

get elevation of a point using Azure maps


I am working on a desktop app and I have been able to bring the map in the WPF window using Azure Maps. I am trying to get the elevation of a given point. I used the API call given in this article: https://techcommunity.microsoft.com/t5/azure-maps/preview-of-new-azure-maps-elevation-service-rest-apis/m-p/1957321/highlight/false#M47

But I am getting the error below when I debug the "HttpResponseMessage response" in my code.

response = {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1,.....

my method goes like below (with the subscription key hidden)

private static string GetElevationFromCoordinates(double latitude, double longitude)
{
    string subscriptionKey = "myHiddenSubscriptionKey";
    string baseUrl = "https://atlas.microsoft.com/elevation/point/json";

    string url = $"{baseUrl}?api-version=1.0&subscription-key={subscriptionKey}&query={latitude},{longitude}";

    using (HttpClient client = new HttpClient())
    {
        HttpResponseMessage response = client.GetAsync(url).Result;
        response.EnsureSuccessStatusCode();

        string responseBody = response.Content.ReadAsStringAsync().Result;

        JObject json = JObject.Parse(responseBody);

        double elevation = json["data"]["elevationInMeters"].Value<double>();
        return $"Elevation: {elevation} meters";
    }
}

Solution

  • For this doc, the Azure Maps Elevation services and Render V2 DEM tiles will be retired on May 5, 2023.

    Refer to this doc for the Azure Maps SDK Elevation client library.

    So, use the Bing Maps Get Elevations service to replace the Azure Maps Elevation services. For pricing and licensing options, view the Bing Maps licensing.

    The code below is for interacting with the Bing Maps Elevations API. You can use the following URL templates to obtain elevation data for points, paths, or areas.

     private static readonly string BingMapsKey = "Your_Bing_Maps_Key";
        
        static async Task Main(string[] args)
        {
            
            string points = "35.89431,-110.72522,35.89393,-110.72578";
            string url = $"http://dev.virtualearth.net/REST/v1/Elevation/List?points={points}&key={BingMapsKey}";
            
         
            var response = await GetElevationDataAsync(url);
       
            if (response != null && response.resourceSets.Length > 0)
            {
                var elevations = response.resourceSets[0].resources[0].elevations;
                Console.WriteLine("Elevations:");
                foreach (var elevation in elevations)
                {
                    Console.WriteLine(elevation);
                }
            }
            else
            {
                Console.WriteLine("No data found.");
            }
        }
    
        private static async Task<ElevationResponse> GetElevationDataAsync(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    HttpResponseMessage response = await client.GetAsync(url);
                    if (response.IsSuccessStatusCode)
                    {
                        string json = await response.Content.ReadAsStringAsync();
                        return JsonConvert.DeserializeObject<ElevationResponse>(json);
                    }
                    else
                    {
                        Console.WriteLine($"Error: {response.StatusCode}");
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Exception: {ex.Message}");
                    return null;
                }
            }
        }
    }
    
    
    public class ElevationResponse
    {
        public ResourceSet[] resourceSets { get; set; }
    }
    
    public class ResourceSet
    {
        public int estimatedTotal { get; set; }
        public ElevationResource[] resources { get; set; }
    }
    
    public class ElevationResource
    {
        public int[] elevations { get; set; }
        public int zoomLevel { get; set; }
    }
    
    
    

    enter image description here