Forum Xamarin.Forms

GPS getPositionAsync (get current location) Implementation isn't working?

SIUSSIUS PSMember
edited June 2016 in Xamarin.Forms

I'm using this plugin https://blog.xamarin.com/geolocation-for-ios-android-and-windows-made-easy/

The app DO NOT CRASHES.. It just leaves the method after this line:
var position = await locator.GetPositionAsync (-1);

I call updateLocation() method in the OnStart of the application, and updateLocation() calls getCurrentLocation() which is inside my ViewModel class:

`

 protected override void OnStart ()
  {
    this.updateLocation ();
  }

public async Task updateLocation()
{
    ViewModels.ListViewModel lvm = new ViewModels.ListViewModel ();
    lvm.Logs = new System.Collections.ObjectModel.ObservableCollection<ViewModels.Log> ();     //Logs is binding to ItemsSource of a listView
    await lvm.getCurrentLocation ();
}

`

`

 public class ListViewModel : BaseViewModel
    {

        Position position;
        public Position Position {
            get {
                return position;
            }
            set {
                position = value;
            }
        }

        Log log;
        public Log Log {
            get {
                return log;
            }
            set {
                log = value;
            }
        }

        public ObservableCollection<Log> Logs {
            get;
            set;
        }

        public ListViewModel ()
        {
            this.Logs = new ObservableCollection<Log> ();
        }

        public async Task getCurrentLocation ()
        {
            var locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;
            var position = await locator.GetPositionAsync (-1);   //

            this.Position = position;
            this.Log = new Log {
                Latitude = this.Position.Latitude,
                Longitude = this.Position.Longitude,
                DateHour = this.Position.Timestamp
            };

            this.Log.Title = "Latitude: " + log.Latitude.ToString () + " \n" + "Longitude: " + log.Longitude.ToString ();
            this.Log.Description = "às " + log.DataHora;
            this.Logs.Add (this.Log);
        }
    }

`

Sign In or Register to comment.