首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Geo编码器- getFromLocation()不推荐使用

Geo编码器- getFromLocation()不推荐使用
EN

Stack Overflow用户
提问于 2022-08-23 10:03:44
回答 4查看 1.6K关注 0票数 2

我收到一条消息,这个函数(或它的构造函数)已被废弃。这个函数有一个新的构造函数,它接受一个附加的参数'Geocoder.GeocodeListener listener',但是这个新的构造函数需要一个33级以上的API。对于较低的API级别,我应该做什么,解决方案是什么?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2022-08-23 10:50:19

由于这在API级别33中是不可取的,我相信这是降低API级别的唯一选择。

票数 4
EN

Stack Overflow用户

发布于 2022-10-22 03:37:47

我认为处理这种弃用的最干净的方法是将getFromLocation移动到新的扩展函数中,并添加如下所示的@ move (“反推荐”):

代码语言:javascript
运行
复制
@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)
    }
}

这就是如何使用:

代码语言:javascript
运行
复制
    Geocoder(requireContext(), Locale("in"))
        .getAddress(latlng.latitude, latlng.longitude) { address: android.location.Address? ->
        if (address != null) {
            //do your logic
        }
    }
票数 2
EN

Stack Overflow用户

发布于 2022-11-22 09:14:06

android.location.Geocoder.GeocodeListener) -使用getFromLocation(double、double、int、)来避免阻塞等待结果的线程。

示例:

代码语言:javascript
运行
复制
//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)

 }

})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73456748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档