async void ObtenerDireccion(double lat, double lon)
        {
            try
            {
                var position          = new Xamarin.Forms.Maps.Position(lat, lon);
                var possibleAddresses = await geoCoder.GetAddressesForPositionAsync(position);

                foreach (var address in possibleAddresses)
                {
                    try
                    {
                        string[] auxDireccion = address.Split(Convert.ToChar("\n"));
                        Direccion = auxDireccion[0];
                        break;
                    }
                    catch
                    {
                        Direccion = "Ups, no te localizamos";
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
            }
        }
Exemple #2
0
        async void ObtenerDireccion(double lat, double lon)
        {
            var position          = new Xamarin.Forms.Maps.Position(lat, lon);
            var possibleAddresses = await geoCoder.GetAddressesForPositionAsync(position);

            foreach (var address in possibleAddresses)
            {
                Direccion = address;
                break;
            }
        }
Exemple #3
0
        /*
         * private void MyMap_MapClicked(object sender, TK.CustomMap.TKGenericEventArgs<TK.CustomMap.Position> e)
         * {
         *  _pins.Clear();
         *  //if(MyMap.Pins != null)  MyMap.Pins.ToList().Clear();
         *  var pin = new TKCustomMapPin
         *  {
         *      //Type =  TKCustomMapPin.Place,
         *      Position = e.Value,
         *      Title = "Posición Actual"
         *      //Label = "Posición Actual",
         *  };
         *
         *  _pins.Add(pin);
         * // MyMap.Pins = _pins;
         *
         * }*/

        async private void MyMap_PinDragEnd(object sender, TK.CustomMap.TKGenericEventArgs <TK.CustomMap.TKCustomMapPin> e)
        {
            double lat = e.Value.Position.Latitude;
            double lng = e.Value.Position.Longitude;

            Xamarin.Forms.Maps.Geocoder geoCoder = new Xamarin.Forms.Maps.Geocoder();
            TK.CustomMap.Position       position = new TK.CustomMap.Position(lat, lng);
            var vm = (MainViewModel)BindingContext;

            vm.Maps.Coordenadas = position.Latitude + "," + position.Longitude;
            var possibleAddresses = await geoCoder.GetAddressesForPositionAsync(new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude));

            vm.Maps.Direccion = "";
            foreach (var a in possibleAddresses)
            {
                vm.Maps.Direccion += a;
                break;
            }
        }
Exemple #4
0
        private async void loadCurrentPosition(bool fromButton)
        {
            //if (fromButton)
            //{
            await Task.Delay(1500);

            //}

            if (!this.IsVisible)
            {
                return;
            }


            try
            {
                var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);

                if (status != PermissionStatus.Granted)
                {
                    if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
                    {
                        await DisplayAlert("Need location", "Gunna need that location", "OK");
                    }

                    var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Location);

                    //Best practice to always check that the key exists
                    if (results.ContainsKey(Permission.Location))
                    {
                        status = results[Permission.Location];
                    }
                }

                if (status == PermissionStatus.Granted)
                {
                    CancellationTokenSource ctx = new CancellationTokenSource();
                    var locator = CrossGeolocator.Current;

                    //CrossGeolocator.Current.PositionChanged += CrossGeolocator_Current_PositionChanged;
                    locator.DesiredAccuracy = 15;
                    if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
                    {
                        var location = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));

                        TK.CustomMap.Position position = new TK.CustomMap.Position(location.Latitude, location.Longitude);

                        // MyMap.MoveToRegion(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(0.25)));

                        _pins.Clear();

                        var pin = new TKCustomMapPin
                        {
                            IsVisible   = true,
                            IsDraggable = true,
                            //Type =  TKCustomMapPin.Place,
                            Position = position,
                            Title    = "Posición Actual"
                                       //Label = "Posición Actual",
                        };

                        _pins.Add(pin);
                        if (MyMap.Pins == null)
                        {
                            MyMap.Pins = _pins;
                        }


                        MyMap.MoveToMapRegion(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(0.25)));

                        Xamarin.Forms.Maps.Geocoder geoCoder = new Xamarin.Forms.Maps.Geocoder();

                        var vm = (MainViewModel)BindingContext;
                        vm.Maps.Coordenadas = position.Latitude + "," + position.Longitude;
                        var possibleAddresses = await geoCoder.GetAddressesForPositionAsync(new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude));

                        vm.Maps.Direccion = "";
                        foreach (var a in possibleAddresses)
                        {
                            vm.Maps.Direccion += a;
                            break;
                        }
                    }
                }
                else if (status != PermissionStatus.Unknown)
                {
                    await DisplayAlert("Localización denegada", "No se puede continuar, intentar nuevamente.", "OK");
                }
            }
            catch (Exception ex)
            {
            }
        }