Get location android Kotlin

前端 未结 6 1675
傲寒
傲寒 2020-12-13 07:28

I recently added get location function. When I try to show longitude and latitude, it returns zero.

This my LocationListener class:

inner class Myloc         


        
6条回答
  •  攒了一身酷
    2020-12-13 07:39

    I read many of answers but question is get only last known location. With receiver it continuously send latitude and longitude I have solution for this in kotlin.. Give permissions

    
    
    
    
    
    
        public fun getLastKnownLocation(context: Context) {
            val locationManager: LocationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
            val providers: List = locationManager.getProviders(true)
            var location: Location? = null
            for (i in providers.size - 1 downTo 0) {
                location= locationManager.getLastKnownLocation(providers[i])
                if (location != null)
                    break
            }
            val gps = DoubleArray(2)
            if (location != null) {
                gps[0] = location.getLatitude()
                gps[1] = location.getLongitude()
                Log.e("gpsLat",gps[0].toString())
                Log.e("gpsLong",gps[1].toString())
    
            }
    
        }
    

提交回复
热议问题