在我的Windows手机应用程序中,我使用以下代码获取当前位置
private void GetCoordinate()
{
var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold = 2
};
watcher.PositionChanged += this.watcher_PositionChanged;
watcher.StatusChanged += this.watcher_StatusChanged;
watcher.Start();
}
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
MessageBox.Show("Current position not available!!");
break;
case GeoPositionStatus.NoData:
MessageBox.Show("Current position not available!!");
break;
}
}
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
try
{
var pos = e.Position.Location;
StaticData.currentCoordinate = new GeoCoordinate(pos.Latitude, pos.Longitude);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
如上所示,我在应用程序页面的页面加载事件中调用GetCoordinate
方法。通过使用GPS
坐标值,我启用或禁用了页面上的某些控件。但在这里,问题是首先执行页面加载代码,然后我们得到Gps
坐标值..
我需要GPS
中的pageload
坐标值。请建议
答案 0 :(得分:0)
您应该在页面加载中启动观察者,并且一旦GeoCoordinateWatcher收集GeoCoordinate值,它就会触发PositionChanged事件,您将在其中编码以启用禁用控件。
所以现在你的代码看起来像这样
private void GetCoordinate()
{
var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High)
{
MovementThreshold = 2
};
watcher.PositionChanged += this.watcher_PositionChanged;
watcher.StatusChanged += this.watcher_StatusChanged;
watcher.Start();
}
private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
try
{
var pos = e.Position.Location;
if(position==required one)
{
//enable disable here
}
StaticData.currentCoordinate = new GeoCoordinate(pos.Latitude, pos.Longitude);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}