我收到一条消息,这个函数(或它的构造函数)已被废弃。这个函数有一个新的构造函数,它接受一个附加的参数'Geocoder.GeocodeListener listener'
,但是这个新的构造函数需要一个33级以上的API。对于较低的API级别,我应该做什么,解决方案是什么?
发布于 2022-08-23 10:50:19
由于这在API级别33中是不可取的,我相信这是降低API级别的唯一选择。
发布于 2022-10-22 03:37:47
我认为处理这种弃用的最干净的方法是将getFromLocation移动到新的扩展函数中,并添加如下所示的@ move (“反推荐”):
@Suppress("DEPRECATION")
fun Geocoder.getAddress(
latitude: Double,
longitude: Double,
address: (android.location.Address?) -> Unit
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getFromLocation(latitude, longitude, 1) { address(it.firstOrNull()) }
return
}
try {
address(getFromLocation(latitude, longitude, 1)?.firstOrNull())
} catch(e: Exception) {
//will catch if there is an internet problem
address(null)
}
}
这就是如何使用:
Geocoder(requireContext(), Locale("in"))
.getAddress(latlng.latitude, latlng.longitude) { address: android.location.Address? ->
if (address != null) {
//do your logic
}
}
发布于 2022-11-22 09:14:06
android.location.Geocoder.GeocodeListener) -使用getFromLocation(double、double、int、)来避免阻塞等待结果的线程。
示例:
//Variables
val local = Locale("en_us", "United States")
val geocoder = Geocoder(this, local)
val latitude = 18.185600
val longitude = 76.041702
val maxResult = 1
//Fetch address from location
geocoder.getFromLocation(latitude,longitude,maxResult,object : Geocoder.GeocodeListener{
override fun onGeocode(addresses: MutableList<Address>) {
// code
}
override fun onError(errorMessage: String?) {
super.onError(errorMessage)
}
})
https://stackoverflow.com/questions/73456748
复制相似问题