{"id":32209,"date":"2016-02-04T16:25:52","date_gmt":"2016-02-04T10:55:52","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=32209"},"modified":"2016-02-09T13:51:55","modified_gmt":"2016-02-09T08:21:55","slug":"find-latitude-and-longitude-of-an-address-location-using-google-api-in-asp-net-using-c","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/find-latitude-and-longitude-of-an-address-location-using-google-api-in-asp-net-using-c\/","title":{"rendered":"Find Latitude and Longitude of an Address Location Using Google API in Asp .Net using c#"},"content":{"rendered":"<p>This Blog explains how to find the Latitude \/ Longitude co-ordinates by using Google Geocoding API. This API will return latitude and longitude with respect to a address of a location in\u00a0<a href=\"http:\/\/www.tothenew.com\/blog\/10-key-basic-things-before-starting-development-on-a-new-asp-net-web-forms-application\/\">Asp .Net<\/a>.<\/p>\n<p>We have to pass locations address as a parameter in address query strings in the Google API URL and returns to the Geographical Co-ordinates and other information in XML or\u00a0<a href=\"http:\/\/www.tothenew.com\/blog\/using-definitions-internal-to-json-schema-and-external-to-json-schema-version-draft-v4\/\">JSON<\/a>\u00a0format.<\/p>\n<p><strong>How to send request to get coordinate by using Geocoding API<\/strong><\/p>\n<p><strong><a href=\"http:\/\/maps.google.com\/maps\/api\/geocode\/xml?address=Parameters&amp;sensor=false\">http:\/\/maps.google.com\/maps\/api\/geocode\/xml?address=Parameters&amp;sensor=false<\/a><\/strong><\/p>\n<p>Through above URL, we can find the latitude and longitude by passing the particular address parameter in query string named \u201caddress\u201d. The output may be either of the following value:<\/p>\n<ul>\n<li>JSON<\/li>\n<li>XML<\/li>\n<\/ul>\n<p>We can use <strong>Google Geocoding API<\/strong> to convert an address to its Latitude or longitude by creating HTTP request.<\/p>\n<p><strong>C# Code<\/strong><\/p>\n<p>Add following namespace<\/p>\n<p>using System.IO;<\/p>\n<p>using System.Net;<\/p>\n<p>Stirng strAddress \u00a0= \u201dNoida 201301,India\u201d;<\/p>\n<p>String url=<strong><a href=\"http:\/\/maps.google.com\/maps\/api\/geocode\/xml?address=\u201d+%20Address%20+\u201d&amp;sensor=false\">http:\/\/maps.google.com\/maps\/api\/geocode\/xml?address=\u201d+ strAddress +\u201d&amp;sensor=false<\/a>;<\/strong><\/p>\n<p><strong>This is given below function named as \u201cGetdtLatLong\u201d in which we will pass input parameter the url of API as given above and it will return \u00a0the output as data table <\/strong><\/p>\n<p>Public DataTable GetdtLatLong(string url)<\/p>\n<p>{<\/p>\n<p>DataTable dtGMap=null;<\/p>\n<p>WebRequest request = WebRequest.Create(url);<\/p>\n<p>using (WebResponse response = (HttpWebResponse)request.GetResponse())<\/p>\n<p>{<\/p>\n<p>using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))<\/p>\n<p>{<\/p>\n<p>DataSet dsResult = new DataSet();<\/p>\n<p>dsResult.ReadXml(reader);<\/p>\n<p>DataTable dtCoordinates = new DataTable();<\/p>\n<p>dtCoordinates.Columns.AddRange(new DataColumn[4] { new DataColumn(&#8220;Id&#8221;, typeof(int)),<\/p>\n<p>new DataColumn(&#8220;Address&#8221;, typeof(string)),<\/p>\n<p>new DataColumn(&#8220;Latitude&#8221;,typeof(string)),<\/p>\n<p>new DataColumn(&#8220;Longitude&#8221;,typeof(string)) });<\/p>\n<p>foreach (DataRow row in dsResult.Tables[&#8220;result&#8221;].Rows)<\/p>\n<p>{<\/p>\n<p>string geometry_id = dsResult.Tables[&#8220;geometry&#8221;].Select(&#8220;result_id = &#8221; + row[&#8220;result_id&#8221;].ToString())[0][&#8220;geometry_id&#8221;].ToString();<\/p>\n<p>DataRow location = dsResult.Tables[&#8220;location&#8221;].Select(&#8220;geometry_id = &#8221; + geometry_id)[0];<\/p>\n<p>dtCoordinates.Rows.Add(row[&#8220;result_id&#8221;], row[&#8220;formatted_address&#8221;], location[&#8220;lat&#8221;], location[&#8220;lng&#8221;]);<\/p>\n<p>}<\/p>\n<p>dtGMap= dtCoordinates;<\/p>\n<p>returns\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0dtGMap;<\/p>\n<p>} }<\/p>\n<p><strong>Result: The output of dtGMap data table which is fetched by google API will be as<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-32471\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/02\/GoogleMapAns.png\" alt=\"GoogleMapAns\" width=\"545\" height=\"132\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This Blog explains how to find the Latitude \/ Longitude co-ordinates by using Google Geocoding API. This API will return latitude and longitude with respect to a address of a location in\u00a0Asp .Net. We have to pass locations address as a parameter in address query strings in the Google API URL and returns to the [&hellip;]<\/p>\n","protected":false},"author":310,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":289},"categories":[1953,1],"tags":[1440,3050,3089],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32209"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/310"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=32209"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/32209\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=32209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=32209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=32209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}