Google Geocode ; Retreive Longitude and Latitude Example

Finding the Latitude and Longitude of an address is very easy now, with this you can mark or locate the place in the google map very easily

You can find the Longitude and Latude of an adress from your windows application which is connected to internet. See the code below to make necessary idea about that. It will return 600,0,0 if the address not found

Dynamic Url building is a part of the process so you must build a URL dynamically to retrieve the Longitude and Latitude

See The Code


public string ConstructGeoCodeURL()
{
string geoURL = "";
string address = "";
string GoogleMapkey = ""; // you must optain a key from google map website
address = "Mathew Philip, Kottayam";
address = address.Trim();
address = address.Replace(" ", "+");
geoURL = @"http://maps.google.com/maps/geo?q=###ADDRESS###&;output=###OUTPUT###&;key=###KEY###";
geoURL = geoURL.Replace("###ADDRESS###", address);
geoURL = geoURL.Replace("###OUTPUT###", CSV);
geoURL = geoURL.Replace("###KEY###", GoogleMapkey);
return geoURL;

}


Next we are moving to retreive the Location



public void LocationGeoCode(string geoURL)
{
string csvValues = "";

try
{
WebRequest objWebRequest = WebRequest.Create(geoURL);
WebResponse objWebResponse = objWebRequest.GetResponse();

Stream objWebStream = objWebResponse.GetResponseStream();

using (StreamReader objStreamReader = new StreamReader(objWebStream))
{
csvValues = objStreamReader.ReadToEnd();
}

if (!(string.IsNullOrEmpty(csvValues)))
{
string[] geoValues = csvValues.Split(new char[] { ',' });

if (geoValues.Length > 0)
{
double Latitude = Convert.ToDouble(geoValues[2].ToString());
double Longitude = Convert.ToDouble(geoValues[3].ToString());
}

}
}
catch
{

}

}


Comments

Author: Palamadai Balaji03 Feb 2010 Member Level: Bronze   Points : 1

hii the LocationGeoCode(string geoURL) is not getting the latitude and longitude I tried..plz help..

Author: kapil29 Jun 2010 Member Level: Bronze   Points : 1

Hi Mathew Philip,

i usedthe above code and its working fine, but sometimes its not returning correct latitude, langitude value if ther is a special character in the address. please suggest how to overcome this issue.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: