【发布时间】:2014-07-09 08:43:06
【问题描述】:
我有一个天气应用程序,它以纬度和经度获取用户的位置,然后使用 api 来返回结果。关闭位置服务后,应用程序会在打开时崩溃,并且不会提供任何错误帮助以查看错误所在。有没有办法编写一个 if 语句来查看位置服务是否是一个?我应该怎么做才能防止这个问题?
代码如下:
async private void GetLocation()
{
var geolocator = new Geolocator();
if (geolocator.LocationStatus == PositionStatus.Disabled)
{
//MessageBox.Show("We need your current location for the app to function properly, please set location services on in settings");
MessageBoxResult mRes = MessageBox.Show("We need your current location for the app to function properly, please set location services on in settings", "I understand", MessageBoxButton.OKCancel);
if (mRes == MessageBoxResult.OK)
{
Application.Current.Terminate();
}
if (mRes == MessageBoxResult.Cancel)
{
Application.Current.Terminate();
}
}
Geoposition position = await geolocator.GetGeopositionAsync();
Geocoordinate coordinate = position.Coordinate;
latitude = Convert.ToString(Math.Round(coordinate.Latitude, 2));
longitude = Convert.ToString(Math.Round(coordinate.Longitude, 2));
URL = "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&units=metric";
Client(URL);
}
public void Client(string uri)
{
var clientToken = new WebClient();
clientToken.OpenReadCompleted += clientToken_OpenReadCompleted;
clientToken.OpenReadAsync(new Uri(uri));
}
感谢您的帮助:)
【问题讨论】:
标签: c# api xaml windows-phone-8 geolocation