setMockMode不起作用 - FusedLocationApi

时间:2017-04-10 16:58:52

标签: android geolocation fusedlocationproviderapi android-fusedlocation

我正在设置模拟位置(使用FusedLocationApi)来测试我的基于地图的应用程序,但它似乎不起作用。

LocationServices.FusedLocationApi.setMockMode(mGoogleApiClient, true);


 Location location = new Location("fused");
            // Time is needed to create a valid Location
            long currentTime = System.currentTimeMillis();
            long elapsedTimeNanos = 0;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
                elapsedTimeNanos = SystemClock.elapsedRealtimeNanos();
                location.setElapsedRealtimeNanos(elapsedTimeNanos);
            }
            location.setTime(currentTime);
            // new york 40.7128° N, 74.0059° W
            location.setLatitude(40.7128);
            location.setLongitude(74.0059);
            PendingResult<Status> s =  LocationServices.FusedLocationApi.setMockLocation(mGoogleApiClient, location);
            s.setResultCallback(new ResolvingResultCallbacks<Status>(MainActivity.this, 100) {
                @Override
                public void onSuccess(@NonNull Status status) {
                    if(status.isSuccess()){
                        log("mock success");

                        if (mIsFromContinuousUpdates) {
                            startContinuousUpdates();
                            return;
                        }
                        getLastKnowLocation();
                    }
                }

                @Override
                public void onUnresolvableFailure(@NonNull Status status) {

                }
            });

PendingResult会返回成功,但是当我致电getLastKnownLocation时,它会返回null并且在我请求持续更新时不会触发onLocationChanged

我在这里遗漏了什么吗?

0 个答案:

没有答案