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
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..!!!
Hi, Can you please share me the code for generating latitude and longitude form address fields.
Thanks in Advance
LikeLike
Hey Srinivasan, I find this code useful for your purpose: http://www.tothenew.com/blog/find-latitude-and-longitude-of-an-address-location-using-google-api-in-asp-net-using-c/ . Let me know if any issue.
LikeLike
hey i would like to ask for your help on how to convert longitude and latitude to cm in coding on visual studio
LikeLike
hi this code retrive me an exception .
System.Net.WebException: ‘Error remote server: (400) request not valid.’
thanks
LikeLike
Hi Vishal,
This code works for me once only second time its giveing BAD SERVER Error.
Please assist.
LikeLike