다른 자산뿐만 아니라 자신의 위치를 매핑하는 전화 응용 프로그램을 만들고 있습니다. 전화는 WI-FI를 통해 다른 자산의 위치와 사용자가 정하는 시간 (일반적으로 5 초)에 대한 업데이트를 가져옵니다. 전화가 약 4 분 동안 얼어 버릴 수있는 문제. 문제를 해결하려면 어떻게해야합니까? 내가 스레딩에 초보자입니다Windows phone map이 계속 충돌합니다
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();
}
}
, 나는 그 문제가 될 수있다 생각 :
이
내 코드입니다. 어떤 도움을 주셔서 감사합니다.이것은 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();
}
}
"GetPhoneGPS()"란 무엇입니까? 어쩌면 그게 그 충돌을 일으키는 원인일까요? 전체 소스를 팝. –