2013-01-22 72 views
1

我想通過GPS的幫助獲取座標,當我將空運行或調試事件使PositionChange StatusChange & StatusChange調用TWICE時。這裏是我的代碼,請幫助我。在WP7中發生兩次PositionChanged&StatusChanged事件

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     if (NetworkInterface.GetIsNetworkAvailable()) 
     { 
      flag = true; 
      if (watcher == null) 
      { 
       watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default); 
       watcher.MovementThreshold = 20; 
       watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged); 
       watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged); 
       watcher.Start(); 
      } 
     } 

    } 


void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) 
    { 
     switch (e.Status) 
     { 
      case GeoPositionStatus.Disabled: 
       MessageBox.Show("Location Service is not enabled on the device"); 
       break; 

      case GeoPositionStatus.NoData: 
       MessageBox.Show(" The Location Service is working, but it cannot get location data."); 
       break; 
     } 
    } 

    void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e) 
    { 
     if (e.Position.Location.IsUnknown) 
     { 
      MessageBox.Show("Please wait while your prosition is determined...."); 
      return; 
     } 

     List<string> locationData = new List<string>(); 
     locationData.Add(e.Position.Location.Latitude.ToString("Latitude:" + "0.000")); 
     locationData.Add(e.Position.Location.Longitude.ToString("Longitude:" + "0.000")); 
     locationData.Add(e.Position.Location.Altitude.ToString()); 
     locationData.Add(e.Position.Location.Speed.ToString()); 
     locationData.Add(e.Position.Location.Course.ToString()); 
} 

回答

1

這就是所謂的兩次,因爲從

狀態更改初始化 - >就緒

初始化它觸發一次,爲了準備第二次:)

+0

所以可以請你提供任何解決方案只能調用一次.. –

+0

基本上它是n事件,並且事件不能被控制,就好像位置從45到50改變一樣,它將觸發儘可能多的次數來檢測變化的位置。現在,如果你只需要一次,那麼你可以刪除你關聯IE的事件:watcher.PositionChanged - = new EventHandler >(watcher_PositionChanged); 然後它會觸發一次並停止。 – Anobik