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
{
}
}
hii the LocationGeoCode(string geoURL) is not getting the latitude and longitude I tried..plz help..