예제 #1
0
		async void GetPosition()
		{
			try 
			{
				locator = new Geolocator{DesiredAccuracy =50};
				if ( locator.IsListening != true )
					locator.StartListening(minTime: 1000, minDistance: 0);
				position = await locator.GetPositionAsync (timeout: 20000);
			}

			catch ( Exception e) 
			{

			}
		}
		protected override void OnResume ()
		{
			base.OnResume ();

			// Get a handle on the map element
			_mapFragment = FragmentManager.FindFragmentById(Resource.Id.map) as MapFragment;
			_map = _mapFragment.Map;

			_map.UiSettings.MyLocationButtonEnabled = true;
			_map.MyLocationEnabled = true;

			_locator = new Geolocator(this) {
				DesiredAccuracy = 1000
			};

			_locator.PositionChanged += OnLocationChanged;
			_locator.StartListening(2000, 1);
		}
        public async Task<LocationCoordinates> GetCurrentLocation()
        {
            EventHandler<PositionEventArgs> handler = null;
            var result = new LocationCoordinates();
            TaskCompletionSource<LocationCoordinates> tcs = new TaskCompletionSource<LocationCoordinates>();
            Geolocator locator = new Geolocator { DesiredAccuracy = 50 };

            try
            {
                if (!locator.IsListening)
                {
                    locator.StartListening(10, 100);    
                }                

                handler = (object sender, PositionEventArgs e) =>
                {
                    result.Status = e.Position.Timestamp;
                    result.Latitude = e.Position.Latitude;
                    result.Longitude = e.Position.Longitude;
                    locator.PositionChanged -= handler;
                    tcs.SetResult(result);
                };
                locator.PositionChanged += handler;                
                
                await locator.GetPositionAsync(timeout: 10000).ContinueWith(
                    t =>
                    {
                    });               
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException.GetType().ToString() == "Xamarin.Geolocation.GeolocationException")
                {
                    tcs.SetException(ex);
                }
            }

            return tcs.Task.Result;
        }
예제 #4
0
 private void UpdateLocation()
 {
     locationManager = new Geolocator (this);
     locationManager.PositionChanged += (sender, e) => {
         location = e.Position;
         locationManager.StopListening ();
     };
     locationManager.StartListening (0, 0);
 }