jmawebtech

xamarinmobile

Nov 7th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. private void Setup()
  2.         {
  3.             if (this.geolocator != null)
  4.                 return;
  5.            
  6.             this.geolocator = new Geolocator (this) { DesiredAccuracy = 50 };
  7.             if (!this.geolocator.IsListening)
  8.             {
  9.                 if (!this.geolocator.IsGeolocationAvailable || !this.geolocator.IsGeolocationEnabled)
  10.                 {
  11. #if DEBUG
  12.                     Toast.MakeText (this, "Geolocation is unavailable", ToastLength.Long).Show();
  13. #endif
  14.                     return;
  15.                 }
  16.                
  17.  
  18.                 this.geolocator.StartListening (minTime: 30000, minDistance: 0);
  19.             }
  20.             else
  21.             {
  22.                 this.geolocator.StopListening();
  23.             }
  24.             //this.geolocator.PositionError += OnListeningError;
  25.             //this.geolocator.PositionChanged += OnPositionChanged;
  26.         }
  27.        
  28.         public void OnGetPosition (Activity act)
  29.         {
  30.             Setup();
  31.  
  32.             this.cancelSource = new CancellationTokenSource();
  33.  
  34.             this.geolocator.GetPositionAsync (timeout: 10000, cancelToken: this.cancelSource.Token)
  35.                 .ContinueWith (t => act.RunOnUiThread (() =>
  36.                 {
  37.                     if(t.IsFaulted == false && t.IsCanceled == false)
  38.                     {
  39.                         CurrentLocation = t.Result;
  40.                         geolocator.StopListening();
  41.  
  42.                     }
  43.                 }));
  44.         }
Advertisement
Add Comment
Please, Sign In to add comment