Results 1 to 2 of 2
  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Location
    Planet Earth
    Posts
    278
    Rep Power
    0

    Default Fused location provider returns same location always

    I have been trying to get my current location updates and used Fused location provider as suggested by Android devs but I am unable to get any update. I have searched thoroughly on web but couldn't find any solution. Can anyone help me out here?
    Java Code:
    public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener ,
            LocationListener, GoogleApiClient.ConnectionCallbacks{
    
        private LocationRequest mLocationRequest;
        private GoogleApiClient mGoogleApiClient;
        private TextView mLocationTextView;
    
        private static final int LOC_PERMISSION_CODE = 100;
        private Location mCurrentLocation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            createLocationRequest();
    
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            mGoogleApiClient.connect();
    
    
            mLocationTextView = (TextView) findViewById(R.id.location);
        }
    
        private boolean isLocationProviderAvailable(){
            LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
            return lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER) || lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }
    
        private void requestLocation() {
            if (!hasLocationPermission()) {
                ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, LOC_PERMISSION_CODE);
            }else {
    
                if(!isLocationProviderAvailable()){
                    AlertDialog dialog = new AlertDialog.Builder(this)
                            .setTitle("No location service")
                            .setMessage("Location adapters are turned off. Please turn on and try again.")
                            .setIcon(R.drawable.location_icon)
                            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    finish();
                                }
                            })
                            .create();
                    dialog.show();
                }else {
                    //noinspection MissingPermission
                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
                }
            }
        }
        private boolean hasLocationPermission(){
            return ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        }
    
        private void createLocationRequest(){
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setInterval(1000 * 10);
            mLocationRequest.setFastestInterval(1000 * 5);
            mLocationRequest.setNumUpdates(2);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
         
            if(requestCode == LOC_PERMISSION_CODE){
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    requestLocation();
                }else {
                    Toast.makeText(this, "Permission Required.", Toast.LENGTH_SHORT).show();
                }
            }
        }
    
    
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            log("onConnectionFailed -> %s", connectionResult.getErrorMessage());
        }
    
        private void log(String format, Object... args){
            Log.d("MainActivity", String.format(format, args));
        }
    
        @Override
        public void onLocationChanged(Location location) {
            mCurrentLocation = location;
            log("onLocationChanged()");
            updateUI();
        }
    
        private void updateUI(){
            log("updateUI()");
            if(null != mCurrentLocation) {
                mLocationTextView.setText(String.format("Lat: %s, Lon: %s", mCurrentLocation.getLatitude(), mCurrentLocation.getLatitude()));
            }
        }
    
        @Override
        public void onConnected(@Nullable Bundle bundle) {
            requestLocation();
            log("onConnected()");
        }
    
        @Override
        public void onConnectionSuspended(int i) {
            log("onConnectionSuspended()");
        }
    }

  2. #2
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Location
    Planet Earth
    Posts
    278
    Rep Power
    0

    Default Re: Fused location provider returns same location always

    Got it. There was a typo.

Similar Threads

  1. Comment Location
    By Googol in forum New To Java
    Replies: 2
    Last Post: 04-18-2013, 12:21 PM
  2. Fixed location
    By Dipke in forum New To Java
    Replies: 4
    Last Post: 08-13-2010, 10:25 AM
  3. JList Location
    By BeeGee in forum Advanced Java
    Replies: 8
    Last Post: 04-07-2010, 03:09 PM
  4. need to copy file from one location to another location
    By naveen.dpt2007 in forum New To Java
    Replies: 4
    Last Post: 02-28-2010, 03:53 PM
  5. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •