2

私はあなたの位置と他の資産をマップする電話アプリケーションを作成しています。電話機は、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();
    }
}

私はスレッディングの初心者ですが、それが問題になるかもしれないと思います。助けてくれてありがとう。

これは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();
        }
    }
4

1 に答える 1

0

これは、エミュレーターを介して使用しているときですか、それとも開発モードでアプリを電話にリリースした後ですか? エミュレーターが更新を好まない場合がいくつかあることがわかりましたが、それらはほとんどありません。

物事を見ると、すべて問題ないように見えます-電話が動いている限り、コードは実行されます。私は自分のアプリで実質的に同じロジックを使用しています。

于 2012-12-17T09:30:21.647 に答える