OnLocationChanged在不同设备上的不同时间间隔触发

时间:2015-10-09 14:22:38

标签: android xamarin

尝试在requestLocationUpdate中指定的时间内获取位置。但是OnLocationChanged事件在不同设备上的不同时间间隔上调用

INITIALIZE LOCATION MANAGER

      private void InitializeLocationManager()
      {      
            latlon.provider = new List<string>();
            _locationManager = (LocationManager)GetSystemService(LocationService);
            //Application criteria for selecting provider
            Criteria criteriaForLocationService = new Criteria { Accuracy = Accuracy.NoRequirement };
            IList<string> acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);


            if (acceptableLocationProviders.Any())
            {
                _locationProvider = acceptableLocationProviders.First();
            }
            else
            {
                _locationProvider = String.Empty;
            }

            if (_locationProvider != null && _locationProvider != string.Empty)
            {
                   //Request for gps in 10sec and 0 distance travelled 
                    _locationManager.RequestLocationUpdates(_locationProvider, 10000, 0, 
             }
       }

位置聆听界面

    async public void OnLocationChanged(Location location)
    {
        try
        {
            _currentLocation = location;
            if (_currentLocation == null)
            {
            }

            else
            {
                 Android.Util.Log.Info("GetLocation", "Latitude: " + _currentLocation.Latitude.ToString() + "        Longitude: " + _currentLocation.Longitude.ToString());

            }

        }

        catch (Exception ex)
        {
            Toast.MakeText(Application.Context, ex.Message, ToastLength.Long).Show();
        }
    }

    public void OnProviderDisabled(string provider)
    {
        try
        {
            InitializeLocationManager();
        }
        catch (Exception ex)
        {
            Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
        }
    }

    public void OnProviderEnabled(string provider)
    {
        try
        {
            InitializeLocationManager();
        }
        catch (Exception ex)
        {
            Toast.MakeText(this, ex.Message, ToastLength.Long).Show();
        }
    }

    public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
    {
        Toast.MakeText(Application.Context, "status Changed", ToastLength.Long).Show();
    }

在Xamarin.android中实现 Thanx提前

1 个答案:

答案 0 :(得分:2)

来自文档:http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)

它说

  

位置更新之间的最短时间间隔,以毫秒为单位

位置更新之间

Minimum time不是exact time。所以不要依赖这个价值。