Android融合位置提供程序:OnLocationChange从未调用过

时间:2016-05-18 07:52:43

标签: java android fusedlocationproviderapi android-fusedlocation

融合位置提供程序api有一个很大的问题,我所有的方法(例如在onConnected我得到的最后一个位置)都在工作,除了OnLocation更改我真的不知道是什么问题,因为我在MapView的其他片段中使用相同的代码,它没有任何问题。 这是我的代码:

    public class MissionsFragment extends Fragment implements 
    SwipeRefreshLayout.OnRefreshListener,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            LocationListener{
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    LatLng latLng;
View view;
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.fragment_jobs, container, false);
            ButterKnife.bind(this, view);
            return view;
        }
     @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            buildGoogleApiClient();
            mGoogleApiClient.connect();
        }


        @Override
        public void onConnected(@Nullable Bundle bundle) {
            Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            if (mLastLocation != null) {
                latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                Toast.makeText(getActivity(), latLng.toString(), Toast.LENGTH_SHORT).show();
            }
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(2000);
            mLocationRequest.setFastestInterval(1000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            //mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter

            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }

        @Override
        public void onConnectionSuspended(int i) {

        }

        @Override
        public void onLocationChanged(Location location) {
            latLng = new LatLng(location.getLatitude(), location.getLongitude());
            Toast.makeText(getActivity(), latLng.toString(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

        }

        protected synchronized void buildGoogleApiClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }

        @Override
        public void onPause() {
            super.onPause();
            if (mGoogleApiClient != null) {
                LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            }
        }
    }

如果我们只使用MapView,或者我应该在当前代码中更新soemthing,是否需要获取位置?

0 个答案:

没有答案