首页 > 解决方案 > 每 1 分钟定位一次

问题描述

我正在创建一个 xamarin 表单应用程序,无论应用程序是在前台还是后台运行,每 1 分钟后需要用户位置(用户的经度和纬度)。如果用户停留在一个位置或缓慢移动,我能够在每 1 分钟后获取用户位置。

但是当用户在骑自行车或高速移动的情况下面临获取位置的问题,我无法在这种情况下获取用户的位置(用户经度和纬度)。

try
                {
                    if (CrossGeolocator.Current.IsGeolocationAvailable)
                    {
                        if (CrossGeolocator.Current.IsGeolocationEnabled)
                        {
                            count += 1;

                            // var location = await GetLocationFromPhone().ConfigureAwait(false);
                            var location = await Geolocation.GetLocationAsync(new GeolocationRequest(GeolocationAccuracy.High, TimeSpan.FromSeconds(10)));
                            if (location != null)
                            {
                            
                                var model = new lat_lon
                                {
                                    lat = location.Latitude,
                                    lon = location.Longitude,
                                    dist = count
                                };
                                MessagingCenter.Send<lat_lon>(model, "LocationLngLet");
                                
                            }
                        }
                    }
                }
               catch (TargetInvocationException srtiex)
                {
                  
                }
                catch (IllegalStateException ilsex)
                {
                  
                }
                catch (FeatureNotEnabledException fneex)
                {
                 
                }
                catch (NullReferenceException nrex)
                {
                
                }
                catch (FeatureNotSupportedException fnsEx)
                {
                }
                catch (PermissionException pEx)
                {
                    // Handle permission exception                
                }
                catch (System.Exception ex)
                {
                    // Unable to get location              
                }

建议任何解决这个问题的方法!!

标签: xamarin.formsxamarin.android

解决方案


这应该在服务中运行(https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/services/https://docs.microsoft.com/en-us/xamarin/ios/ app-fundamentals/backgrounding/ios-backgrounding-walkthroughs/location-walkthrough),因为无论手机/应用程序的状态如何,您都需要它工作。考虑为每个项目使用 LocationManagers 并设置 locationChanged 所需的属性。


推荐阅读