using System;
public class Test
{
public static void Main()
{
// your code goes here
}
public static async Task<DbGeography> GetLatLongForAddress(string location)
{
float[] latLong = new float[2];
var add = string.Format("http://m...content-available-to-author-only...s.com/maps/api/geocode/xml?address={0}&sensor=false", location.Replace("#", "%23"));
string result;
using (var hc = new HttpClient())
{
return await (await hc.GetStringAsync(add).ContinueWith(async task =>
{
if (task.IsFaulted)
{
var ex = task.Exception;
}
result = task.Result;
var doc = XDocument.Parse(result);
string status = doc.Element("GeocodeResponse").Element("status").Value;
//no errors occurred; the address was successfully parsed and at least one geocode was returned.
if (status.Equals("OK"))
{
var point = doc.Element("GeocodeResponse").Element("result").
Element("geometry").Element("location");
string lat = point.Element("lat").Value;
string lng = point.Element("lng").Value;
latLong[0] = (float)Convert.ToDouble(lat);
latLong[1] = (float)Convert.ToDouble(lng);
}
else if (status.Equals("ZERO_RESULTS"))
{
//geocode was successful but returned no results
throw new ApplicationException("No maps found for this address");
}
else if (status.Equals("REQUEST_DENIED"))
{
//request was denied
throw new ApplicationException("Request Denied");
}
else if (status.Equals("INVALID_REQUEST"))
{
//address is missing
throw new ApplicationException("Address not found");
}
else if (status.Equals("UNKNOWN_ERROR"))
{
//the request could not be processed due to a server error
//throw new ApplicationException("Unknown Error. Try agian.");
return await GetLatLongForAddress(location);
}
var ret = DbGeography.FromText(string.Format("POINT({1} {0})", latLong[0], latLong[1]));
return ret;
}));
}
}
}