Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Getting more precise location

Posted on November 11, 2022 By

Solution 1 :

Somethings you should know about getLastKnownLocation(...) [From Document]

Gets the last known location from the given provider, or null if there is no last known location. The returned location may be quite old in some circumstances, so the age of the location should always be checked.

This will never activate sensors to compute a new location, and will only ever return a cached location.


Please try locationManager.requestSingleUpdate(...), this method will request user current location.

if(locationManager.allProviders.contains(LocationManager.GPS_PROVIDER)) {
    locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locListener)
} else if(locationManager.allProviders.contains(LocationManager.NETWORK_PROVIDER)){
    locationManager.rerequestSingleUpdate(LocationManager.NETWORK_PROVIDER, locListener)
}

And this is listener.

val locListener = object: LocationListener {
        override fun onLocationChanged(location: Location?) {
            // do something with updated location here.
        }

        override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onProviderEnabled(provider: String?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onProviderDisabled(provider: String?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    }

Problem :

In my Android Application I am targeting the device ranging from,

Minimum Android Version – 7.1 (API 25)

Target Android Version – 10.0 (API 29)

I am using the below logic to get the location Information

Permissions Using

<uses-permission android_name="android.permission.INTERNET"/>
<uses-permission android_name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android_name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android_name="android.permission.ACCESS_NETWORK_STATE"/>

Code Logic

 LocationManager 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
        && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    try {
        gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         network_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (gps_loc != null) {
            final_loc = gps_loc;
            latitude = final_loc.getLatitude();
            longitude = final_loc.getLongitude();
        }
        else if (network_loc != null) {
            final_loc = network_loc;
            latitude = final_loc.getLatitude();
            longitude = final_loc.getLongitude();
        }
        else {
            latitude = 0.0;
            longitude = 0.0;
        }
    }

But most of the time I am not getting accurate location with API call. But If enable the below option in device level (in Phone), I am getting the most accurate location

READ  [FIXED] java - Android: Edit views added programmatically
Powered by Inline Related Posts

enter image description here

Is there any way I can configure the above settings in application level through code. The client which I am working is not satisfied with changing the configuration manually.

I am wondering how I can achieve the same through code. Someone please help.

Comments

Comment posted by Stephan Ronald

answer is not relevant to the question, he was asking about enabling wifi scanning of Google location service through code.

Comment posted by Markus Kauppinen

This does make some sense though as when you are just relying on

Comment posted by Hello World

From the questuin, I think that what he want is precise location so I advice hime to use

Android Tags:android, google-location-services

Post navigation

Previous Post: [FIXED] android – How to pass data by Back Button, Kotlin
Next Post: [FIXED] Project with external module issue in android studio 3.6

Related Posts

[FIXED] java – Firebase realtime database multipath updates vs firestore database batched write Android
[FIXED] android – Kotlin DatePicker Pop-Up doesn’t work inside fragment : WindowManager$BadTokenException: Unable to add window — token null is not valid; Android
[FIXED] java – Android Firebase Query for Multiple values Android
[FIXED] Is it possible to read SMS in Android NOT for verification code? Android
[FIXED] android – Filter ArrayList multiple times in Java Android
[FIXED] kotlin – Will the same value trigger the event in LiveDate? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • How do you stop a number from calling you permanently?
  • What are the main features of Islamic education?
  • Is the Jeep 4xe worth it?
  • How does the ringer work on cast iron?
  • What is the biggest size interior door?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme