Не могу получить локацию
Не хочет выводить координаты, в чем может быть проблема ?
class MyProfile : AppCompatActivity() {
private lateinit var ttextVie: TextView
private lateinit var locationManager: LocationManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_my_profile)
ttextVie = findViewById(R.id.textView)
locationManager = getSystemService(LOCATION_SERVICE) as LocationManager
}
override fun onResume() {
super.onResume()
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
ttextVie.text = "нет разрешений"
return
} else {
locationManager?.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
1L,
1f,
locationListener
)
}
}
override fun onPause() {
super.onPause()
locationManager.removeUpdates(locationListener)
}
private val locationListener: LocationListener = object : LocationListener {
override fun onLocationChanged(location: Location) {
ttextVie.text = ("" + location.longitude + ":" + location.latitude)
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
override fun onProviderEnabled(provider: String) {}
override fun onProviderDisabled(provider: String) {}
}
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
implementation 'com.google.android.gms:play-services-location:17.0.0'
Разрешения на местоположения в настройках включил
Ответы (1 шт):
Автор решения: Andrew
→ Ссылка
У себя в проекте я использую такой способ:
private var mFusedLocationClient: FusedLocationProviderClient? = null
private var mLocationRequest: LocationRequest? = null
private fun checkSelfLocation(){
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
val permissions = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
ActivityCompat.requestPermissions(this, permissions, 0)
} else {
mLocationRequest = LocationRequest()
mLocationRequest?.interval = 10000
mLocationRequest?.fastestInterval = 3000
mLocationRequest?.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
mFusedLocationClient?.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper())
}
}
private var mLocationCallback: LocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
if (locationResult.locations.isNotEmpty()) {
val locIndex = locationResult.locations.size - 1
val lon = locationResult.locations[locIndex].longitude
val lat = locationResult.locations[locIndex].latitude
if (!sp.contains("last_location") || sp.getString(
"last_lon",
""
) != "$lon" || sp.getString(
"last_lat",
""
) != "$lat"
) {
sp.edit().putString("last_lon", "$lon").apply()
sp.edit().putString("last_lat", "$lat").apply()
init(lat, lon)
}
}
}
}
и дальше остановка обновления местоположения делается так:
mFusedLocationClient?.removeLocationUpdates(mLocationCallback)
как можно понять из кода, это:
val lon = locationResult.locations[locIndex].longitude
val lat = locationResult.locations[locIndex].latitude
ваша локаль которую можно дальше использовать где вам нужно.