Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Wating at DependencyService.Get().GetPositionAsync(10000)

$
0
0

Here I need to get the Current Position of the user. I'm using the following code to get the Position

public async Task getMyCurrentPosition()
        {
            try {
                IGeolocator geolocator = DependencyService.Get<IGeolocator>();
                Position result = null;

                if (geolocator.IsGeolocationEnabled) {
                    try {
                        if (!geolocator.IsListening)
                            geolocator.StartListening(1000, 1000);
                            result = await geolocator.GetPositionAsync(10000);

                        System.Diagnostics.Debug.WriteLine("[GetPosition] Lat. : {0} / Long. : {1}", result.Latitude.ToString("N4"), result.Longitude.ToString("N4"));
                    }
                    catch (Exception e) {
                        System.Diagnostics.Debug.WriteLine ("Error : {0}", e);
                    }
                }
                if (result != null){
                    Session.Lattitude = result.Latitude;
                    Session.Longitude = result.Longitude;
                }
                    //return calculateDistance (result.Latitude, result.Longitude, this.Lattitude, this.Longitude, 'M');
            } catch (Exception ex) {
                Debug.WriteLine (ex.Message);
            }
        }

I'm calling this is in a synchronous method using

Task.Run( asyn() => {await getMyCurrentPosition()}).Wait(); it is waiting waiting... ... not getting the result.

If I use directly with getMyCurrentPosition() I'm getting the result. But the execution goes to next statement before getting the result.

Please help me out this issue on how to wait till the result comes.

Thanks in Advance


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>