0

Here is my code

    private var longitude = ""
    private var latitude = ""
 val lm = activity?.getSystemService(Context.LOCATION_SERVICE) as LocationManager?
        if (ActivityCompat.checkSelfPermission(
                requireActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                requireActivity(),
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {

        }
        val location = lm!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
        longitude = location!!.longitude.toString()
        latitude = location.latitude.toString()

So location is null, How can I get my lat et long correctly in Kotlin?

mathdx
  • 45
  • 6
  • I'm in a fragment. – mathdx Apr 12 '22 at 21:06
  • 1
    GPS needs time to warm up doesn't it. Have you tried calling it again later? What if there is no last known location, that sounds like a null. – Blundell Apr 12 '22 at 21:14
  • Yeah it's my fist time. But How can I get my location then if not by getLastKnownLocation? I've try getCurrentLocation but I don't know wich parameter to pass... – mathdx Apr 12 '22 at 21:26
  • This question has been asked about a million times and there are tons of answers/suggestions/approaches. There are also hundreds of tutorials/howtos/examples on the Interwebs. Please spend a bit of time searching before you post a question like this. – David Wasser Apr 12 '22 at 21:58
  • Also, the documentation of `getLastKnownLocation()` clearly states that it can return `null` and that it will only return a cached location value (it will not activate sensors like GPS). See https://developer.android.com/reference/android/location/LocationManager#getLastKnownLocation(java.lang.String) – David Wasser Apr 12 '22 at 22:00
  • Check my code here: https://stackoverflow.com/questions/71849853/unable-to-get-longitude-and-latitude-kotlin-android I've check some tuto but it's not working and I don't know why – mathdx Apr 12 '22 at 22:53

0 Answers0