地理编码器不断返回 true

发布于 2024-12-12 06:18:55 字数 1023 浏览 0 评论 0原文

我的应用程序使用 GeoCoder 不断将字符串返回为 true。这是我的代码。

public boolean address(){
      Geocoder geoCoder = 
            new Geocoder
            (getBaseContext(), Locale.getDefault());

            try {
                List<Address> addresses = geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6, LocationManagerHelper.getLongitude()/1E6, 1);

                String addes = "";
                if (addresses.size() > 0) 
                {
                    for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                         i++)
                       addes += addresses.get(0).getAddressLine(i) + "\n";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }
            return true;


 }

我将其称为文本视图。

add.append(""+ address()
            + '\n');

它有点工作,只是它不显示地址,而是显示 true

任何人都可以告诉我这是为什么吗?

谢谢。

My application using a GeoCoder keeps returning the string to true. here is the code I have.

public boolean address(){
      Geocoder geoCoder = 
            new Geocoder
            (getBaseContext(), Locale.getDefault());

            try {
                List<Address> addresses = geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6, LocationManagerHelper.getLongitude()/1E6, 1);

                String addes = "";
                if (addresses.size() > 0) 
                {
                    for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                         i++)
                       addes += addresses.get(0).getAddressLine(i) + "\n";
                }
            }
            catch (IOException e) {                
                e.printStackTrace();
            }
            return true;


 }

and I call that into a text view.

add.append(""+ address()
            + '\n');

it works somewhat except for it doesn't show the address and instead shows true

can any one tell me why this is?

thanks.

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

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

发布评论

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

评论(2

玩世 2024-12-19 06:18:55

您需要使用 return 关键字返回地址字符串,并将方法返回类型更改为 String:

public String address(){
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
    String result = "";

    try {
        List<Address> addresses = geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6, LocationManagerHelper.getLongitude()/1E6, 1);

        if (addresses.size() > 0) 
        {
            for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
               result+= addresses.get(0).getAddressLine(i) + "\n";
        }
    }
    catch (IOException e) {                
        e.printStackTrace();
    }

    return result;
 }

You need to return the address string by using the return keyword, and changing your method return type to a String:

public String address(){
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
    String result = "";

    try {
        List<Address> addresses = geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6, LocationManagerHelper.getLongitude()/1E6, 1);

        if (addresses.size() > 0) 
        {
            for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
               result+= addresses.get(0).getAddressLine(i) + "\n";
        }
    }
    catch (IOException e) {                
        e.printStackTrace();
    }

    return result;
 }
吃兔兔 2024-12-19 06:18:55

您需要在 try外部初始化结果 String 变量,然后将方法的返回类型更改为 String 并在使用地址中的行对其进行扩充后,确实会返回结果 String 变量。

public String address() {
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
    String result = "";
    try {
        List<Address> addresses =
            geoCoder.getFromLocation(LocationManagerHelper.getLatitude() / 1E6,
                                     LocationManagerHelper.getLongitude() / 1E6,
                                     1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                    result += address.getAddressLine(i) + "\n";
            }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

但是,实际上,这是基本的 Java 编程,因此在深入研究 Android 等框架之前,我衷心建议您先学习该语言。

You need to initialize the result String variable outside the try block, then change the method's return type to String and indeed return the result String variable after having augmented it with the lines from the address.

public String address() {
    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
    String result = "";
    try {
        List<Address> addresses =
            geoCoder.getFromLocation(LocationManagerHelper.getLatitude() / 1E6,
                                     LocationManagerHelper.getLongitude() / 1E6,
                                     1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                    result += address.getAddressLine(i) + "\n";
            }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

But, really, this is basic Java programming, so before delving into a framework such as Android I would heartily suggest you learn the language first.

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