Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
723 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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());
}
}
}
}
}
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;
}
}
}
}