0

Hola amigos estoy tratando de obtener la geolocaliacion de mi telefono con Xamarin.Maps.Forms y Mvvm , pero me sale este error. Entiendo que no puedo convertir el metodo como tal en una position, pero entonces alguien sabe como traerlo de mi view model a mi page.cs ??

introducir la descripción de la imagen aquí

Este es mi codigo

 [XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapPage : ContentPage
{
    private MapPageViewModel _viewModel;
    public MapPage()
    {
        InitializeComponent();
        BindingContext = _viewModel = new  MapPageViewModel();
    }

    protected  override void OnAppearing()
    {
        base.OnAppearing();

        var currentLocation =  _viewModel.GetCurrentLocationCommand;
        EJMap.Pins.Clear();
        Pin Current = new Pin() 
        {
            Position = currentLocation, //aqui esta el error de arriba
        };
       
    }
}

Este es mi view model

 public class MapPageViewModel
{        
    #region props

        public ICommand GetCurrentLocationCommand { get; set; }
    #endregion
    public MapPageViewModel()
    {
        GetCurrentLocationCommand = new Command(async ()=> await GetCurrentLocationCommandExecute());
    }

    public async Task<Position> GetCurrentLocationCommandExecute() 
    {
        Position currentPosition = new Position();
        CancellationTokenSource cts;
        try
        {
            var location = await Geolocation.GetLastKnownLocationAsync();
            if (location != null)
            {
                 currentPosition = new Position(location.Latitude, location.Longitude);
            }
            else 
            {
                var request = new GeolocationRequest(GeolocationAccuracy.High, TimeSpan.FromSeconds(10));
                cts = new CancellationTokenSource();
                var currentLocation = await Geolocation.GetLocationAsync(request, cts.Token);

                if (currentLocation != null)
                {
                     currentPosition = new Position(currentLocation.Latitude, currentLocation.Longitude);
                }

            }
        }
        catch (Exception)
        {

            throw;
        }
        return currentPosition;
    } 
}
2
  • currentPosition obtiene algo? el 21 jul. 2021 a las 23:01
  • si lo obtienes podrias enviarlo a traves de un evento, con la clase MessaginCenter, pero podrias usar una propiedad con PropertyChanged se me ocurre. el 21 jul. 2021 a las 23:09

1 respuesta 1

1

Una idea que se me ocurre como te dije en el comentario es usar un evento con la clase MessaginCenter, donde te suscribis a un evento y debe haber un receptor y un emisor.

En el constructor de tu View podes hacer esto:

//variable dentro de tu view.cs
 public string CurrentPosition {get;set;}

  //dentro del constructor para que quede suscripto el evento.
  MessagingCenter.Subscribe<string, string>("Mapas", "EnviarPosicion", async (codigo, LectorCodigo) =>
  {
      var currentLocation =  EnviarPosicion;
      EJMap.Pins.Clear();
      Pin Current = new Pin() 
      {
        Position = currentLocation
      };
   });

//En tu metodo donde obtenes la posicion realizas el envio mediante el evento.

        if (location != null)
        {
             currentPosition = new Position(location.Latitude, location.Longitude);
        

          MessagingCenter.Send<string, string>("Mapas", "EnviarPosicion", currentPosition.ToString());
        }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.