1

I have a big problem with fused location provider api , all the methods I ovveride (for example in onConnected I get the last location known) are working except OnLocation change I don't know really what's the problem because I use the same code in other fragment with MapView and it works without any problem. This is my code :

    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);
            }
        }
    }

is it necessary to get the location if we use MapView only or I should update soemthing in my current code ?

AndroLife
  • 908
  • 2
  • 11
  • 27
  • did u try moving the device upto some distance and then got the onLocationChanged Refer here you might have missed something http://coderzpassion.com/android-location-using-google-play-services/ – Jagjit Singh May 18 '16 at 08:13
  • @JagjitSingh the problem is with the same device I call another activity and the code works but in this current activity it dosen't work at all. – AndroLife May 18 '16 at 08:18
  • Check if the device has googleplayservices or if location is enabled? – Jagjit Singh May 18 '16 at 08:20
  • I'm using the emualtor (android 5.1) , location is enabled (I get the last known location) <= OnConnected works – AndroLife May 18 '16 at 08:22
  • ok then you pushing custom latlong for that? – Jagjit Singh May 18 '16 at 08:24
  • @JagjitSingh I didn't understand what do you mean ? I send the location to the device using default google emulator options and like I said it works with another fragment perfectly (I even tried gps animate movement) – AndroLife May 18 '16 at 08:26

0 Answers0