Android GPS Not Accurate (AGNA)


AGNA:   SOLD       




Hi Dev, has anyone ever tried to create a location feature on an Android project? I am currently using the opportunity to rewrite my code into Kotlin, from the previous Java. Actually, I am not only rewriting the code, but also optimizing it, so that the performance of the application I created is better.

The problem arises when my marker point is far from my current location, it can be up to 900-600 meters. My question is how to write the correct code so that I can get a precise location point?

As an illustration, this is roughly what my code looks like.

package com.agam.mapslocation;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MapsActivity extends Activity {

    private static final int ONE_MINUTE = 1000 * 60 * 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        final LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        final EditText et = (EditText) findViewById(R.id.editText1);

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location l) {
                // Called when a new location is found by the network location
                // provider.
                Location gps = locationManager
                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                Location net = locationManager
                        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                Location bestLocation = null;
                bestLocation = isBetterLocation(gps, net);
                bestLocation = isBetterLocation(bestLocation, l);
                if(bestLocation!=null)
                    displayLocation(et, bestLocation);
            }

            public void onStatusChanged(String provider, int status,
                    Bundle extras) {

            }

            public void onProviderEnabled(String provider) {
                if (provider.equals(LocationManager.GPS_PROVIDER)) {
                    et.setText("GPS ON!");
                }
            }

            public void onProviderDisabled(String provider) {
            }
        };

        // Register the listener with the Location Manager to receive location
        // updates
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, locationListener);
    }

    public static void displayLocation(View v, Location l) {
        ((EditText) v).setText(String.format(
                "Long:%s,\nLat:%s,\nAccu:%s,\nTime ago:%s,\nProvider:%s",
                l.getLongitude(), l.getLatitude(), l.getAccuracy(),
                new java.util.Date().getTime() - l.getTime(), l.getProvider()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_maps, menu);
        return true;
    }

    /**
     * Determines whether one Location reading is better than the current
     * Location fix
     * 
     * @param location
     *            The new Location that you want to evaluate
     * @param currentBestLocation
     *            The current Location fix, to which you want to compare the new
     *            one
     */
    protected Location isBetterLocation(Location location,
            Location currentBestLocation) {
        if (currentBestLocation == null) {
            // A new location is always better than no location
            return location;
        }

        // Check whether the new location fix is newer or older
        long timeDelta = location.getTime() - currentBestLocation.getTime();
        boolean isSignificantlyNewer = timeDelta > ONE_MINUTE;
        boolean isSignificantlyOlder = timeDelta < -ONE_MINUTE;
        boolean isNewer = timeDelta > 0;

        // If it's been more than two minutes since the current location, use
        // the new location
        // because the user has likely moved
        if (isSignificantlyNewer) {
            return location;
            // If the new location is more than two minutes older, it must be
            // worse
        } else if (isSignificantlyOlder) {
            return currentBestLocation;
        }

        // Check whether the new location fix is more or less accurate
        int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
                .getAccuracy());
        boolean isLessAccurate = accuracyDelta > 0;
        boolean isMoreAccurate = accuracyDelta < 0;
        boolean isSignificantlyLessAccurate = accuracyDelta > 200;

        // Check if the old and new location are from the same provider
        boolean isFromSameProvider = isSameProvider(location.getProvider(),
                currentBestLocation.getProvider());

        // Determine location quality using a combination of timeliness and
        // accuracy
        if (isMoreAccurate) {
            return location;
        } else if (isNewer && !isLessAccurate) {
            return location;
        } else if (isNewer && !isSignificantlyLessAccurate
                && isFromSameProvider) {
            return location;
        }
        return currentBestLocation;
    }

    /** Checks whether two providers are the same */
    private boolean isSameProvider(String provider1, String provider2) {
        if (provider1 == null) {
            return provider2 == null;
        }
        return provider1.equals(provider2);
    }
}

After consulting, it turns out that there are many factors that influence the results obtained.

  1. Make sure you are using the correct location provider such as GPS or Network, even better if you use both.
  2. Make sure you get permission from the user to access GPS, or at least the Android smartphone device used has a built-in GPS device.
  3. Also make sure that you are not using Wifi, because the physical location of the Wifi modem is not necessarily around you, it could be that the device is far from where you are. Cases like the topic above are generally most often found at this point.
  4. Also make sure that your smartphone device has Google service installed, because this is the starting point to help GPS recalculate the location, so you don't need to start again from 0, but can use the history recorded by Google service.

To understand how Android gets location, and how to identify fixed and incorrect ordinates, I have summarized it into a few key points.

GPS chipset

  • The results are definitely precise
  • The process of getting points is a little slower
  • Cannot tolerate building block obstacles, meaning there is little chance of getting a precise location when you are inside a building or obstructed by trees.
  • Requires GPS chipset.
  • Can run in airplane mode.
  • Depends on the number of satellites that can be captured

MACs Wifi (modem)

  • Sometimes the results are confusing depending on where you are connected to the wifi. The accuracy can be up to 10-100m.
  • The location determination process is quite fast
  • Need to activate wifi feature
  • Need to consume data package
  • You need Google service installed on your Android device (some cheap devices may not be able to install this service)

Network Provider (relative to your mobile phone number SIM Card)

  • The results are quite precise, even though the distance between your device and the transmitter tower (BTS) is around hundreds of meters, or tens of kilometers.
  • The location determination process is very fast.
  • Need to activate SIM Card.

Post a Comment

Previous Next

نموذج الاتصال