public Coordinates GetCoordinatesUsingGoogle(string location) { Coordinates test = new Coordinates(); string url = "http://maps.google.com/maps/api/geocode/xml?address=" + location + "&sensor=false"; WebRequest request = WebRequest.Create(url); using (WebResponse response = (HttpWebResponse)request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { DataSet dsResult = new DataSet(); dsResult.ReadXml(reader); foreach (DataRow row in dsResult.Tables["result"].Rows) { string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString(); DataRow locationTable = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0]; test.Latitude = locationTable["lat"].ToString(); test.Longitude = locationTable["lng"].ToString(); } } } return test; }
public Coordinates GetCoordinates(string location) { Coordinates coordinates = new Coordinates(); GeoCoding.IGeoCoder googleCoder = new GeoCoding.Google.GoogleGeoCoder(); var address = googleCoder.GeoCode(location); if (address != null) { var firstOrDefaultEntry = address.FirstOrDefault(); if (firstOrDefaultEntry != null) { var coordinateEntry = firstOrDefaultEntry.Coordinates; if (coordinateEntry != null) { coordinates.Latitude = coordinateEntry.Latitude.ToString(); coordinates.Longitude = coordinateEntry.Longitude.ToString();// string.Format("{0},{1}", coordinateEntry.Latitude.ToString(), coordinateEntry.Longitude.ToString()); } } } return coordinates; }