Android GPS location accuracy issue

前端 未结 3 1088
青春惊慌失措
青春惊慌失措 2021-01-30 09:16

I am working on gps tracking apps in android. Here is my code architecture:

  1. BackgroundSyncService : A service class that is used for getting locat
3条回答
  •  没有蜡笔的小新
    2021-01-30 09:27

    Location Updates can be done in following ways:

    1. Location you get from GPS is more accurate than the location you get from the network,so if you need accuracy use GPS

    2. Not sure about this but i think its about the context what type of location you want like you want the accuracy or the fastest one so according to that choose your provider

    3. FusedLocation provider is the most efficient and effective solution for getting the location on android now,it gathers the location data from different sources and according to the parameters passed like in how much time you want location to be updated,you want high accuracy or not etc.It provides you the best location.

    4. Yes you should use the FusedLocationProvider for getting the location on android,as google also recommends this and it the most effective and efficient way there to get location on android for now.

    Why don't you try by creating Criteria.ACCURACY_HIGH

    A constant indicating a high accuracy requirement - may be used for horizontal, altitude, speed or bearing accuracy. For horizontal and vertical position this corresponds roughly to an accuracy of less than 100 meters.

    Android tries to mask the reality of the hardware from us, but in practice there are only two location providers: GPS and network. GPS location is slow and not all devices have it, but it is accurate. Network location is faster and is supported by almost all devices, but it is less accurate.

    Google improved its map and location management with fused location provider.

    The main entry point for interacting with the fused location provider.The methods must be used in conjunction with a GoogleApiClient.


    Code:

     new GoogleApiClient.Builder(context)
             .addApi(LocationServices.API)
             .addConnectionCallbacks(this)
             .addOnConnectionFailedListener(this)
             .build()
    

    gps –> (GPS, AGPS): Name of the GPS location provider. This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission android.permission.ACCESS_FINE_LOCATION.

    network –> (AGPS, CellID, WiFi MACID): Name of the network location provider. This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.


    Other Options:

    getLastKnownLocation()

    The getLastKnownLocation() this returns the last known location...after you re-install the app it will not have any last known location...so it will result in NPE...instead use below code

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);
    
            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                //here is the if-else change so code avoids falling into both loops
             // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                } else if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        return location;
    }
    

    Check few Best examples I have found:

    • http://rdcworld-android.blogspot.in/2012/01/get-current-location-coordinates-city.html

    • https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi

    • https://developer.android.com/reference/android/location/LocationManager.html

    • https://demonuts.com/current-gps-location/

    • https://developer.android.com/training/location/retrieve-current.html

    It worked for me...should work for you too...

提交回复
热议问题