【问题标题】:How to save current location?如何保存当前位置?
【发布时间】:2020-03-19 02:57:02
【问题描述】:

我有一个问题。我正在开发移动应用程序,我的用户可以通过登录当前位置存储旅行体验。

我现在已连接到 SQLite,我正在探索 James Monemagno 的 Geolocator nuget。到目前为止,我可以在地图上找到当前位置的图钉,但我不确定如何将位置存储在数据库中。我想它不会是“位置”,它必须是纬度和经度,但是我将如何再次获得带有显示在我的帖子下的图钉的图片?各位有经验吗?

Public clas NoteViewMode : BaseViewModel
{
/////
private string _location;
        public string Location
        {
            get { return _location; }
            set
            {
                _location  = value;
                OnPropertyChanged();
            }
        }

        public double _latitude;
        public double Latitude
        {
            get { return _latitude; }
            set
            {
                _latitude = value;
                OnPropertyChanged();
            }
        }

        private double _longitude;
        public double Longitude
        {
            get { return _longitude; }
            set
            {
                _longitude = value;
                OnPropertyChanged();
            }
        }
}

      public Map()
                {
                    InitializeComponent();
                    GetPremissions();
                    BindingContext = ViewModel = new AdLogEntryViewModel();
                }
                private async void GetPremissions()
                {
                    try
                    {
                        var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse);
                        if (status != Plugin.Permissions.Abstractions.PermissionStatus.Granted)
                        {
                            if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse))
                            {
                                await DisplayAlert("We need location", "", "Ok");
                            }
                            var result = await CrossPermissions.Current.RequestPermissionsAsync(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse);
                            if (result.ContainsKey(Plugin.Permissions.Abstractions.Permission.LocationWhenInUse))
                                status = result[Plugin.Permissions.Abstractions.Permission.LocationWhenInUse];
                        }
                        if (status == Plugin.Permissions.Abstractions.PermissionStatus.Granted)
                        {
                            locationsMap.IsShowingUser = true;
                            hasLocationPermission = true;


                            GetLocation();
                        }
                        else
                        {
                            await DisplayAlert("Location denied", "", "");
                        }

                    }

                    catch (Exception ex)
                    {
                        await DisplayAlert("erroe", ex.Message, "ok");
                    }
                }
                protected override async void OnAppearing()
                {
                    base.OnAppearing();

                    if (hasLocationPermission)
                    {
                        var locator = CrossGeolocator.Current;

                        locator.PositionChanged += Locator_PositionChanged;
                        await locator.StartListeningAsync(TimeSpan.Zero, 100);
                    }

                    GetLocation();
                }
                protected override void OnDisappearing()
                {
                    base.OnDisappearing();

                    CrossGeolocator.Current.StopListeningAsync();
                    CrossGeolocator.Current.PositionChanged -= Locator_PositionChanged;
                }
                void Locator_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
                 {
                    MoveMap(e.Position);
                }
                private async void GetLocation()
                {
                    if (hasLocationPermission)
                    {
                        var locator = CrossGeolocator.Current;
                        var position = await locator.GetPositionAsync();
                        MoveMap(position);


                    }
                }
                private async void MoveMap(Position position)
                {
                    var center = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude);
                    var span = new Xamarin.Forms.Maps.MapSpan(center, 1, 1);
                    locationsMap.MoveToRegion(span);

                }

【问题讨论】:

  • 您可以通过编程方式将图钉添加到您的地图。您的链接图片是否指的是相当令人反感的阴谋论?
  • 诺诺诺,我只是谷歌发布地图
  • 我什至没有看内容,只看到一个帖子和一张地图,我将立即删除链接。我不想得罪任何人
  • 所以,如果我有自己的 pin,那么我可以在没有纬度和经度的 pin 下保存位置?
  • 啊,你已经做到了..你很快。谢谢

标签: xamarin.forms


【解决方案1】:

您可以通过编程方式添加Pin to a Map

Pin pin = new Pin
{
  Label = "Santa Cruz",
  Address = "The city with a boardwalk",
  Type = PinType.Place,
  Position = new Position(36.9628066, -122.0194722)
};
map.Pins.Add(pin);

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2018-05-24
    相关资源
    最近更新 更多