地理编码android - 根据坐标查找地址

发布于 2025-01-07 18:11:51 字数 950 浏览 8 评论 0原文

我在我的 Android 应用程序中组合了这个小函数:

protected void toAddress(double lat, double lng, int maxResults) {

        String address = null;

        Geocoder gcd = new Geocoder(TarsActivity.this, Locale.getDefault());
        List<Address> addresses = null;
        try {
            addresses = gcd.getFromLocation(lat, lng, maxResults);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (addresses.size() > 0) {
            address = addresses.get(0).getLocality().toString();
            Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG ); 
        }
        else
            Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG); 

    }

函数调用如下所示

 toAddress(location.getLatitude(), location.getLongitude(), 10); 

但应用程序不显示任何带有地址的 Toast?我已经对其进行了调试,正确的地址位于地址列表中。正如您所看到的,我尝试使用 toString() 函数将其转换为字符串。有人吗?提前致谢!

I've put together this little function in my android app:

protected void toAddress(double lat, double lng, int maxResults) {

        String address = null;

        Geocoder gcd = new Geocoder(TarsActivity.this, Locale.getDefault());
        List<Address> addresses = null;
        try {
            addresses = gcd.getFromLocation(lat, lng, maxResults);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (addresses.size() > 0) {
            address = addresses.get(0).getLocality().toString();
            Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG ); 
        }
        else
            Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG); 

    }

The function call goes like this

 toAddress(location.getLatitude(), location.getLongitude(), 10); 

But the app doesn't display any Toast with the address? I've debugged it and the correct address is in the addresses-list. As you can see I've tried to convert it to a string with the toString()-function. Anybody? Thanks in advance!

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

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

发布评论

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

评论(1

回心转意 2025-01-14 18:11:51

尝试:

Toast.makeText(getApplicationContext(), 地址, Toast.LENGTH_LONG ).show();

创建 Toast 后,您不会调用 .show()

Try:

Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG ).show();

You are not calling .show() after you create the Toast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文