Search code examples
androidlatitude-longitudeandroid-gps

How to increase the accuracy of the GPS coordinates?


Currently, I am using GPS coordinates to get the user's address as follows, private void findAddress() {

Geocoder geocoder;
List<Address> addresses;
geocoder=new Geocoder(this, Locale.getDefault());

        try{
            addresses=geocoder.getFromLocation(lattitude,longitude,1);
            String address=addresses.get(0).getAddressLine(0);
            String city=addresses.get(0).getLocality();
            String state=addresses.get(0).getAdminArea();
            String country=addresses.get(0).getCountryName();

            countryEt.setText(country);
            stateEt.setText(state);
            cityEt.setText(city);
            addressEt.setText(address);


        } catch (IOException e) {
            Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
        }

    }

location permissions are as follows,

 private void detectLocation() {
        Toast.makeText(this, "Please Wait..", Toast.LENGTH_LONG).show();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    }

This code is completely ok. But the accuracy is low. How can I increase my accuracy. What are the modifications to take place? I am very glad if someone shows me the correct flow.


Solution

  • If you are at the pc, I think you are in a building, in which the gps signal is not fine. Try to go out on the street. In addition you can set requestLocationUpdateswith a listener and make timed requests every 1-1.5 seconds until the accuracy is below 50 meters or 30... at the end unregister the listener. Here is a sample:

    @SuppressLint("MissingPermission")
    private Task<Void> startMyLocationUpdates(int interval) {
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(ctx);
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(interval);   //1,5 seconds
    mLocationRequest.setFastestInterval(interval);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    return mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());