FusedLocationProviderClient需要长达30秒的时间来检索第一次位置更新。

问题描述 投票:1回答:1

当我第一次启动我的应用程序时,FusedLocationProviderClient需要长达30秒的时间来开始提供位置更新(即在蓝点显示之前长达30秒)。我有一个封装在LiveData中的FusedLocationProvider,如下图所示,我试图找出问题所在,但却找不到。

public class LocationLiveData extends LiveData<Location> {

    private final static int LOCATION_REQUEST_INTERVAL_SECONDS = 5;
    private final static int MIN_DISPLACEMENT_REQUEST_METRES = 0;
    private final String TAG = this.getClass().getName();

    private final Context context;

    private FusedLocationProviderClient fusedLocationProviderClient = null;
    private LocationCallback locationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            setValue(locationResult.getLastLocation());
        }
    };

    public LocationLiveData(Context context) {
        this.context = context;
    }

    @Override
    protected void onActive() {
        super.onActive();
        FusedLocationProviderClient locationProviderClient = getFusedLocationProviderClient();
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(TimeUnit.SECONDS.toMillis(LOCATION_REQUEST_INTERVAL_SECONDS));
        locationRequest.setSmallestDisplacement(MIN_DISPLACEMENT_REQUEST_METRES);
        locationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
        Log.d(TAG, "Getting location...");
    }

    @NonNull
    private FusedLocationProviderClient getFusedLocationProviderClient() {
        if (fusedLocationProviderClient == null) {
            fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
        }
        return fusedLocationProviderClient;
    }

    @Override
    protected void onInactive() {
        if (fusedLocationProviderClient != null) {
            fusedLocationProviderClient.removeLocationUpdates(locationCallback);
        }
    }
}

这是我的片段,它持有另一个MapFragment,并在它上调用getMapAsync()。下面是相关的代码。

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_map, viewGroup, false);

        mBinding.setView(this);

        mBinding.setViewModel(ViewModelProviders.of(this).get(MapsViewModel.class));

        FragmentManager fragmentManager = getChildFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) fragmentManager
                .findFragmentById(R.id.map_fragment);
        mapFragment.getMapAsync(this);

        return mBinding.getRoot();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(false);
        mMap.setInfoWindowAdapter(new InfoWindowAdapter(getLayoutInflater()));
        animateCameraToUserLocation();
    }
java android gps location fusedlocationproviderapi
1个回答
0
投票

你可以做的是在gps位置被检索之前,你可以使用缓存的位置与 getLastLocation() 这将给你最后的已知位置,并将比等待GPS信号,并得到更新的位置,你总是可以把一个定时器,如果在5秒内的位置还没有被获取的GPS,你可以使用。getLastLocation()

https:/developer.android.comtraininglocationretrieve-current.html#GetLocation。

© www.soinside.com 2019 - 2024. All rights reserved.