4

I am developing location tracker application in android. When

    public void onLocationChanged(Location location) {
    System.out.println(location);
    }
    so it gives meLocation[mProvider=network,mTime=1302690466456,mLatitude=21.1596426,mLongitude=72.8072426
,mHasAltitude=false,mAltitude=0.0,mHasSpeed=false,mSpeed=0.0,mHasBearing=false,mBearing=0.0
,mHasAccuracy=true,mAccuracy=75.0,mExtras=Bundle[mParcelledData.dataSize=148]]

I want to get horizontal as well as vertical accuracy.. How to get these 2 values.. Thanks in advance

Olegas
  • 10,349
  • 8
  • 51
  • 72
sneha
  • 41
  • 1
  • 3

4 Answers4

1

It's been 8 years 3 months. Probably you have found the solution. It may help others like me who is looking for a help in getting horizonal and vertical accuracy.

In Android Location documentation, it states that

  • getAccuracy() Get the estimated horizontal accuracy of this location, radial, in meters.
  • getVerticalAccuracyMeters() Get the estimated vertical accuracy of this location, in meters.
  • You may find all the location related services and functionalities in the following link managed by Android

https://developer.android.com/reference/android/location/Location

=======

Farruh Habibullaev
  • 2,342
  • 1
  • 26
  • 33
1

I do wanted achieve the same objective, well now you can achieve that and many other information which will be available in the location object which you may receive in LocationCallback. I have enabled a Service for fetching location, so LocationCallback is placed in my Serivce Class LocationService

For this, you have to call the LocationCallback object as stated below :

private lateinit var locationCallback: LocationCallback

locationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult) {
            if (locationResult.lastLocation != null) {
                val location = locationResult.lastLocation
                val intent = Intent(ACTION_LOCATION_UPDATE)
                // Add the current location as an extra to the intent
                intent.putExtra(EXTRA_LOCATION, location)
                val latitude = locationResult.lastLocation!!.latitude
                val longitude = locationResult.lastLocation!!.longitude
                Log.d("LAT_LOND","Current Position : $latitude | $longitude")
                sendBroadcast(intent)

            }
        }
    }

Now you have to call the callback using FusedLocationProviderClient

private lateinit var fusedLocationClient: FusedLocationProviderClient

  // Initialize fusedLocationClient
  fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

 //Calling the locationCallback object

  fusedLocationClient.requestLocationUpdates(
        LocationUtils.getLocationRequest(),
        locationCallback,
        null
    )

Here, as you can see I have passed the whole object of location in a BroadcastReceiver because I wanted to display the information into an activity. Attaching the Activity code as well for your reference.

private val receiver = object : BroadcastReceiver() {
    @RequiresApi(Build.VERSION_CODES.O)
    @SuppressLint("SetTextI18n")
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == LocationService.ACTION_LOCATION_UPDATE) {
            val location = intent.getParcelableExtra<Location>(LocationService.EXTRA_LOCATION)
            // Updating the UI with the new location data
            binding.tvLat.text = location?.latitude.toString().trim()
            binding.tvLong.text = location?.longitude.toString().trim()
            binding.tvAcc.text = "${location?.accuracy.toString().trim()} meters in horizontal"
            binding.tvAccVer.text =
                "${location?.verticalAccuracyMeters.toString().trim()} meters in vertical"
            binding.tvTime.text = convertLongToTime(location?.time!!)
        }
    }
}

You can see that the object of locationCallback holds all the data, from this you can retrieve the information you need.

Also, keep a note in mind that you should enable all the required permissions for fetching location and receiving data of the location.

For better understanding, I am sharing a link where you can enable runtime permissions from the user and interact with the Application, so that the application does not behave improper.

LINK - Runtime Location Permissions Android

Feel free to comment your thoughts on this and do suggest any changes if needed.

Vishal Shah
  • 220
  • 3
  • 11
0

There are no vertical or horizontal accuracy. Accuracy is applied to position at all.

Olegas
  • 10,349
  • 8
  • 51
  • 72
  • 1
    there is vetcial accuracy, but android does not forward this info from the GPS chip. – AlexWien May 30 '13 at 21:54
  • There IS such a thing as vertical vs. horizontal accuracy, when it comes to GPS positioning. Without going into the maths - think about the fact that for GPS device to show altitude it usually needs one more satellite then the minimum for horizontal positioning. – Jonan Gueorguiev Oct 27 '15 at 08:38
-1

It is possible to get horizontal accuracy read it.

http://developer.android.com/reference/android/location/LocationProvider.html

Returns a constant describing horizontal accuracy of this provider. If the provider returns finer grain or exact location, ACCURACY_FINE is returned, otherwise if the location is only approximate then ACCURACY_COARSE is returned.

DynamicMind
  • 4,240
  • 1
  • 26
  • 43
  • This is for when we select provider if we choose only GPS and then want to find vertical and horizontal accuracy at that time this is not useful – Siddhpura Amit Oct 15 '12 at 05:45