什么导致我的不完整位置对象错误?

p5cysglq  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(295)

我正在尝试在android应用程序上创建一个模拟gps服务。我经常遇到以下错误:

java.lang.IllegalArgumentException: incomplete location object, missing timestamp or accuracy?

我看不出我的location对象是如何不完整的,因为我在对象中设置了时间戳和精度。

static LocationManager lm;
    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setMock(String mocLocationProvider, double latitude, double longitude){

        lm.addTestProvider(mocLocationProvider,
                false,
                false,
                false,
                false,
                true,
                true,
                true,
                1,
                2);

        Location loc = new Location(mocLocationProvider);
        Location mockLocation = new Location(mocLocationProvider); // a string
        mockLocation.setLatitude(latitude);  // double
        mockLocation.setLongitude(longitude);
        mockLocation.setAltitude(loc.getAltitude());
        mockLocation.setTime(System.currentTimeMillis());
        mockLocation.setAccuracy(1);
        mockLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        mockLocation.setBearingAccuracyDegrees(0.1F);
        mockLocation.setVerticalAccuracyMeters(0.1F);
        mockLocation.setSpeedAccuracyMetersPerSecond(0.01F);
        lm.setTestProviderEnabled(mocLocationProvider, true);
        lm.setTestProviderLocation(mocLocationProvider, loc);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public int onStartCommand(Intent intent, int flags, int startId) {
                 lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                double[] rand_coords = random_location_generator();
                rand_gen_lat  = rand_coords[0];
                rand_gen_long = rand_coords[1];

                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE );

                setMock(LocationManager.GPS_PROVIDER, rand_gen_lat, rand_gen_long);
                setMock(LocationManager.NETWORK_PROVIDER, rand_gen_lat, rand_gen_long);

                startMockLocationUpdates();

        return START_STICKY;

    }

正如你所看到的,我已经设置了我的准确度和时间戳,那么为什么我仍然得到这个错误呢?我希望我提供了足够的信息来做出正确的诊断。谢谢您。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题