我正在创建一个电话应用程序,用于映射您的位置以及其他资产。手机通过 WI-FI 以及用户选择的设定时间(通常为 5 秒)获取其他资产位置的更新。手机会在 4 分钟左右冻结的问题。我能做些什么来修复它?
这是我的代码:
public bool CenterOnUser { get; set; }
public MapView()
{
InitializeComponent();
CenterOnUser = false;
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
_continue = true;
mapBing.Children.Add(_mapLayer);
_time = Convert.ToInt32(PhoneApplicationService.Current.State["time"]);
_thread = new Thread(Update) {Name = "Map Update"};
_thread.Start();
foreach (var asset in DataManager.GetPhoneAssetDictionary())
{
_averageLatitude += double.Parse(asset.Value.Latitude);
_averageLongitude += double.Parse(asset.Value.Longitude);
}
_averageLatitude /= DataManager.GetPhoneAssetDictionary().Count;
_averageLongitude /= DataManager.GetPhoneAssetDictionary().Count;
mapBing.Center = new GeoCoordinate(_averageLatitude, _averageLongitude);
}
public void Update()
{
while (_thread.IsAlive && _continue)
{
_mapLayer.Dispatcher.BeginInvoke(delegate
{
foreach (var asset in DataManager.GetPhoneAssetDictionary())
{
_position = new GetPhoneGPS().Positions();
// Find out if we already have a pin on the map for this asset
var pin = _mapLayer.Children.Cast<Pushpin>().FirstOrDefault(assetPin => assetPin.Tag.ToString() == asset.Key);
if (pin == null) // do we have a pin
{
// We don't yet. Create one
pin = new Pushpin {Tag = asset.Key};
// Figure out the name to give the pin
if (asset.Key == _position.UniqueId)
{
pin.Content = "You are here";
}
else
{
pin.Content = String.IsNullOrEmpty(asset.Value.Name) ? asset.Key : asset.Value.Name;
}
// Add the pin for this asset to the map
_mapLayer.Children.Add(pin);
}
// Always update the pin location
pin.Location = new GeoCoordinate(double.Parse(asset.Value.Latitude), double.Parse(asset.Value.Longitude));
}
});
Thread.Sleep(_time * 1000);
}
}
private void CenterOnUserBtn_Click(object sender, RoutedEventArgs e)
{
CenterOnUser = true;
mapBing.Center = new GeoCoordinate(double.Parse(_position.Latitude), double.Parse(_position.Longitude));
mapBing.ZoomLevel = 15;
}
private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
{
mapBing.Children.Remove(_mapLayer);
_continue = false;
_thread.Join();
}
}
我是 Threading 的新手,我相信这可能是问题所在。感谢您的任何帮助。
这是getPhone 的代码。
public class GetPhoneGPS : IDisposable
{
private static readonly Positions Position = new Positions();
readonly GeoCoordinateWatcher _watch = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
private void UpdatePhonePosition()
{
_watch.MovementThreshold = 1;
_watch.Start();
_watch.PositionChanged += GeoPositionChange;
var position = _watch.Position;
//loads phones gps coordinates
if (!position.Location.IsUnknown)
{
Position.Latitude = position.Location.Latitude.ToString(CultureInfo.InvariantCulture);
Position.Longitude = position.Location.Longitude.ToString(CultureInfo.InvariantCulture);
Position.Altitude = position.Location.Altitude.ToString(CultureInfo.InvariantCulture);
Position.Speed = position.Location.Speed.ToString(CultureInfo.InvariantCulture);
}
Position.Date = DateTime.Now;
Position.Name = PhoneApplicationService.Current.State["username"].ToString();
Position.UniqueId = PhoneApplicationService.Current.State["UniqueId"].ToString();
}
private void GeoPositionChange(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var position = e.Position;
//loads phones gps coordinates
if (!position.Location.IsUnknown)
{
Position.Latitude = position.Location.Latitude.ToString(CultureInfo.InvariantCulture);
Position.Longitude = position.Location.Longitude.ToString(CultureInfo.InvariantCulture);
Position.Altitude = position.Location.Altitude.ToString(CultureInfo.InvariantCulture);
Position.Speed = position.Location.Speed.ToString(CultureInfo.InvariantCulture);
}
Position.Date = DateTime.Now;
}
public Positions Positions()
{
UpdatePhonePosition();
return Position;
}
public void Dispose()
{
_watch.Dispose();
}
}