reverse geocoding using nominatim in asp.net

7.9k Views Asked by At

I'm using nominatim for reverse geocoding in my asp.net website (Visual Studio 2010, C#).

I'm getting answer in XML or json format on another webpage like below:

I cant read this address using XML reader or HTTP response.

Need help to convert this text into plain text and displaying on my website.

3

There are 3 best solutions below

11
Vladimir Gondarev On BEST ANSWER

Well, you have to deserialize json data that you got from the web Service. You have to define two new classes, namely:

 [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; }
        }

After that you will able to get the data by using this code:

WebClient webClient = new WebClient();
            var jsonData = webClient.DownloadData("http://nominatim.openstreetmap.org/reverse?format=json&lat=23.02951&lon=72.48689");

            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
            var rootObject = ser.ReadObject(new MemoryStream(jsonData));

rootObject contains all data that you need, in order to convert it to plain text or any other format.

0
user4911112 On

To resolved Forbidden error i have added header with WebClient object as below-

WebClient webClient = new WebClient(); 

**webClient.Headers.Add("User-Agent: Other");**

 var jsonData = webClient.DownloadData("http://nominatim.openstreetmap.org/reverse?format=json&lat=23.02951&lon=72.48689");

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RootObject));
var rootObject = ser.ReadObject(new MemoryStream(jsonData));
0
Nuno Ribeiro On

To complement the example given by Vladimir Gondarev, Nominatim currently does not accept requests without Referer and User-Agent. That way, just add these two lines:

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");

(Modify to your liking).