How to get address from longitude and latitude in c# | Reverse geo-coding in c#

Converting longitude and latitude into full address in c#

 

In this post we’ll discuss how you can get address from longitude and latitude. This process is also called reverse geo-coding.

This can be used in many cases. Our example will be used in fetching address from geocodes in crm.

We’ll create a console application and ultimately a crm plugin to convert geocodes to full address.

Creating console App:-

First create a model class  address as below:-

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
namespace reversegeocoding
{
 [DataContract]
 public class Address
 {
 [DataMember]
 public string road { get; set; }
 [DataMember]
 public string suburb { get; set; }
 [DataMember]
 public string city { get; set; }
 [DataMember]
 public string state_district { get; set; }
 [DataMember]
 public string state { get; set; }
 [DataMember]
 public string postcode { get; set; }
 [DataMember]
 public string country { get; set; }
 [DataMember]
 public string country_code { get; set; }
 }

[DataContract]
 public class RootObject
 {
 [DataMember]
 public string place_id { get; set; }
 [DataMember]
 public string licence { get; set; }
 [DataMember]
 public string osm_type { get; set; }
 [DataMember]
 public string osm_id { get; set; }
 [DataMember]
 public string lat { get; set; }
 [DataMember]
 public string lon { get; set; }
 [DataMember]
 public string display_name { get; set; }
 [DataMember]
 public Address address { get; set; }
 }
}

 

Main class:-

using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
namespace reversegeocoding
{
 class Program
 {
 static void Main(string[] args)
 {
 RootObject rootObject=getAddress(23.5270797, 77.2548046);
 Console.WriteLine("Full Address "+rootObject.display_name);
 }

public static RootObject getAddress(double lat,double lon)
 {
 WebClient webClient = new WebClient();
 webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
 webClient.Headers.Add("Referer", "http://www.microsoft.com");
 var jsonData = webClient.DownloadData("http://nominatim.openstreetmap.org/reverse?format=json&lat="+lat+"&lon="+lon);
 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
 RootObject rootObject = (RootObject)ser.ReadObject(new MemoryStream(jsonData));
 return rootObject;
}
}
}

 

Run your console app. Result is below

10.png

Console app solution is available here

 

I have also developed a plugin to fetch address from lat and long in CRM.

You can download plugin solution from here

Happy Tracking..!!!

5 thoughts on “How to get address from longitude and latitude in c# | Reverse geo-coding in c#

Add yours

  1. Hi, Can you please share me the code for generating latitude and longitude form address fields.

    Thanks in Advance

    Like

  2. hey i would like to ask for your help on how to convert longitude and latitude to cm in coding on visual studio

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: