当我测试这段代码时:
Console.WriteLine(SwearJarController.GetLocation());
我得到错误:
SwearJarController 指的是一个单独的项目,下面是相关代码:
static string baseUri = "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";
public static string GetLocation()
{
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
var myPosition = watcher.Position;
double latitude = -45.856633;
double longitude = 170.500646;
if (!myPosition.Location.IsUnknown)
{
latitude = myPosition.Location.Latitude;
longitude = myPosition.Location.Longitude;
}
RetrieveFormatedAddress(latitude, longitude);
return currentLocation;
}
public static void RetrieveFormatedAddress(double latitude, double longitude)
{
string strlat = Convert.ToString(latitude);
string strlon = Convert.ToString(longitude);
string requestUri = string.Format(baseUri, strlat, strlon);
WebClient wc = new WebClient();
{
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(requestUri));
}
}
public static string currentLocation = "Unknown";
static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
var xmlElm = XElement.Parse(e.Result);
var status = (from elm in xmlElm.Descendants()
where elm.Name == "status"
select elm).FirstOrDefault();
if (status.Value.ToLower() == "ok")
{
var res = (from elm in xmlElm.Descendants()
where elm.Name == "formatted_address"
select elm).FirstOrDefault();
currentLocation = res.Value;
}
else
{
currentLocation = "Unknown";
}
}
catch
{
currentLocation = "Unknown";
}
}
这可能是因为控制器是 WP7 应用程序,但测试器是控制台应用程序?
谢谢!