android geocoder void getfromlocation 如何获取地址?

发布于 2025-01-13 03:09:35 字数 325 浏览 0 评论 0原文

我的android活动中有地理编码器类,其中包含谷歌地图

我需要使用

getFromLocation(double latitude, double longitude, int maxResults, Geocoder.GeocodeListener listener)

此方法反转地理编码具有无效声明,但必须返回地址列表,根据谷歌的说法,他们说要执行以下操作

提供了一个地址数组,试图描述紧邻给定纬度和经度周围的区域。返回的地址应该针对提供给此类构造函数的区域设置进行本地化。

如果此方法是 void 类型,如何获取地址列表?

i have geocoder class in my android activity which contains google map

i need to reverse the geocode using

getFromLocation(double latitude, double longitude, int maxResults, Geocoder.GeocodeListener listener)

this method has void declaration but must returns list of addresses, according to google they said to do the below

Provides an array of Addresses that attempt to describe the area immediately surrounding the given latitude and longitude. The returned addresses should be localized for the locale provided to this class's constructor.

how to do that to get list of addresses if this method is void type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒与心事 2025-01-20 03:09:35

(Kotlin)

在实现抽象方法 onGeocode() 时,地址列表将可用。要访问地址列表,您应该声明一个带有 GeocodeListener 实例实现的变量:

val geocodeListener = @RequiresApi(33) object : Geocoder.GeocodeListener {
    override fun onGeocode(addresses: MutableList<Address>) {
        // do something with the addresses list
    }
}

或使用 lambda 形式:

val geocodeListener = Geocoder.GeocodeListener { addresses ->
    // do something with the addresses list
}

之后,您调用 getFromLocation() 方法一个 Geocoder 实例,用 Android SDK 检查包围它,并为其提供您之前实现的对象:

val geocoder = Geocoder(context, locale)
if (Build.VERSION.SDK_INT >= 33) {
    // declare here the geocodeListener, as it requires Android API 33
    geocoder.getFromLocation(latitude, longitude, maxResults, geocodeListener)
} else {
    val addresses = geocoder.getFromLocation(latitude, longitude, maxResults)
    // For Android SDK < 33, the addresses list will be still obtained from the getFromLocation() method
}

(Kotlin)

The list of addresses will be available when implementing the abstract method onGeocode(). To access the list of addresses you should declare a variable with an implementation for the GeocodeListener instance:

val geocodeListener = @RequiresApi(33) object : Geocoder.GeocodeListener {
    override fun onGeocode(addresses: MutableList<Address>) {
        // do something with the addresses list
    }
}

or using the lambda form:

val geocodeListener = Geocoder.GeocodeListener { addresses ->
    // do something with the addresses list
}

After that, you call the getFromLocation() method on a Geocoder instance, surrounding it with an Android SDK check, and providing it the object that you implemented earlier:

val geocoder = Geocoder(context, locale)
if (Build.VERSION.SDK_INT >= 33) {
    // declare here the geocodeListener, as it requires Android API 33
    geocoder.getFromLocation(latitude, longitude, maxResults, geocodeListener)
} else {
    val addresses = geocoder.getFromLocation(latitude, longitude, maxResults)
    // For Android SDK < 33, the addresses list will be still obtained from the getFromLocation() method
}
等待圉鍢 2025-01-20 03:09:35
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
suspend fun getCityStateName(location : Location?) : String? =
    suspendCancellableCoroutine<String?> { cancellableContinuation ->
        location?.let { loc ->
            Geocoder(context).getFromLocation(
                loc.latitude, loc.longitude, 1
            ) { list -> // Geocoder.GeocodeListener
                list.firstOrNull()?.let { address ->
                    cancellableContinuation.resumeWith(
                        Result.success(
                            "${address.locality} ${address.adminArea}"
                            )
                        )
                    }
            }
        }
    }
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
suspend fun getCityStateName(location : Location?) : String? =
    suspendCancellableCoroutine<String?> { cancellableContinuation ->
        location?.let { loc ->
            Geocoder(context).getFromLocation(
                loc.latitude, loc.longitude, 1
            ) { list -> // Geocoder.GeocodeListener
                list.firstOrNull()?.let { address ->
                    cancellableContinuation.resumeWith(
                        Result.success(
                            "${address.locality} ${address.adminArea}"
                            )
                        )
                    }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文