Get address using longitude and latitude using free bing api

Anonymous
2023-07-15T11:13:37.4+00:00
using System;
using System.Collections.Generic;
using System.Linq;

//Here is the code to get latitude and longitude , from this I want to get adress using bing api please suggest


using System.Text;
using System.Threading.Tasks;
using System.Device.Location;

namespace ConsoleApplicationGeo
{
    class Program
    {
        private static GeoCoordinateWatcher Watcher;

        static void Main(string[] args)
        {
            Watcher = new GeoCoordinateWatcher();
            Watcher.StatusChanged += Watcher_StatusChanged;
            Watcher.Start();

            Console.ReadLine();

        }

        static void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            if (e.Status == GeoPositionStatus.Ready)
            {
                if (Watcher.Position.Location.IsUnknown)
                {
                    Console.Write("Cannot find location");
                }
                else
                {
                    Console.WriteLine("Lat: " + Watcher.Position.Location.Latitude.ToString());
                    Console.WriteLine("Lon: " + Watcher.Position.Location.Longitude.ToString());
                }
            }
        }

    }
}
Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
664 questions
Windows Maps
Windows Maps
A Microsoft app that provides voice navigation and turn-by-turn driving, transit, and walking directions.
253 questions
0 comments No comments
{count} votes

Accepted answer
  1. Vahid Ghafarpour 20,500 Reputation points
    2023-07-15T20:42:01.0233333+00:00

    You can use the reverse geocoding functionality to retrieve the address from latitude and longitude coordinates using the Bing Maps API. Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Device.Location;
    
    namespace ConsoleApplicationGeo
    {
        class Program
        {
            private static GeoCoordinateWatcher Watcher;
            private static string BingMapsApiKey = "YOUR_BING_MAPS_API_KEY";
    
            static async Task Main(string[] args)
            {
                Watcher = new GeoCoordinateWatcher();
                Watcher.StatusChanged += Watcher_StatusChanged;
                Watcher.Start();
    
                Console.ReadLine();
            }
    
            static async void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
            {
                if (e.Status == GeoPositionStatus.Ready)
                {
                    if (Watcher.Position.Location.IsUnknown)
                    {
                        Console.Write("Cannot find location");
                    }
                    else
                    {
                        Console.WriteLine("Lat: " + Watcher.Position.Location.Latitude.ToString());
                        Console.WriteLine("Lon: " + Watcher.Position.Location.Longitude.ToString());
    
                        string address = await ReverseGeocode(Watcher.Position.Location.Latitude, Watcher.Position.Location.Longitude);
                        Console.WriteLine("Address: " + address);
                    }
                }
            }
    
            static async Task<string> ReverseGeocode(double latitude, double longitude)
            {
                using (HttpClient client = new HttpClient())
                {
                    string url = $"https://dev.virtualearth.net/REST/v1/Locations/{latitude},{longitude}?o=json&key={BingMapsApiKey}";
                    HttpResponseMessage response = await client.GetAsync(url);
                    string json = await response.Content.ReadAsStringAsync();
    
                    // Parse the JSON response to extract the address
                    dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                    string address = data.resourceSets[0].resources[0].address.formattedAddress;
    
                    return address;
                }
            }
        }
    }
    
    

0 additional answers

Sort by: Most helpful