2

Hi I am working on a app and i need to fetch the exact (accurate,to the extent) latitude and longitude. The method i used is LocationListener but the issue is, each and every time i am getting different numbers for the same location - say almost a variance of 800 meters.Is there a way i can get the exact numbers each and every time? Below is the code. Please help me in this

public Location getlocation(){

    if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        Toast.makeText(context,"Permission Not Granted",Toast.LENGTH_LONG).show();
        return null;
    }

    LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    boolean isGPSenabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (isGPSenabled){
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,1,this);
        Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        return l;
    }
    else {
        Toast.makeText(context,"Please Enable GPS",Toast.LENGTH_LONG).show();
    }
    return null;

}
Pradeep Kumar
  • 51
  • 1
  • 6

1 Answers1

0

Use Fused API

googleApiClient = new GoogleApiClient.Builder(FusedLocation1.this)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(FusedLocation1.this)
                        .addOnConnectionFailedListener(FusedLocation1.this)
                        .build();

                locationRequest = LocationRequest.create();
                locationRequest.setInterval(2000);

                locationRequest.setFastestInterval(2000);
                locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


                googleApiClient.connect();
Pawan Soni
  • 860
  • 8
  • 19