Java 地理编码器不断返回真值

Java 地理编码器不断返回真值,java,android,geocoding,Java,Android,Geocoding,我的应用程序使用GeoCoder不断将字符串返回为true。这是我的密码 public boolean address(){ Geocoder geoCoder = new Geocoder (getBaseContext(), Locale.getDefault()); try { List<Address> addresses = geoCoder.getFro

我的应用程序使用
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;


 }
除了不显示地址,而是显示
true

谁能告诉我这是为什么


谢谢。

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

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;
 }
公共字符串地址(){
Geocoder Geocoder=新的地理编码器(getBaseContext(),Locale.getDefault());
字符串结果=”;
试一试{
列表地址=geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6,LocationManagerHelper.getLatitude()/1E6,1);
如果(地址.size()>0)
{

对于(int i=0;i您需要使用return关键字返回地址字符串,并将方法返回类型更改为字符串:

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;
 }
公共字符串地址(){
Geocoder Geocoder=新的地理编码器(getBaseContext(),Locale.getDefault());
字符串结果=”;
试一试{
列表地址=geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6,LocationManagerHelper.getLatitude()/1E6,1);
如果(地址.size()>0)
{

对于(int i=0;i您需要在
try
块外初始化result
String
变量,然后将方法的返回类型更改为
String
,并在使用地址中的行进行增广后返回result
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;
}
公共字符串地址(){
Geocoder Geocoder=新的地理编码器(getBaseContext(),Locale.getDefault());
字符串结果=”;
试一试{
列出地址=
geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6,
LocationManagerHelper.getLongitude()/1E6,
1);
如果(地址.size()>0){
地址=地址。获取(0);
对于(int i=0;i

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

您需要在
try
块外初始化result
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;
}
公共字符串地址(){
Geocoder Geocoder=新的地理编码器(getBaseContext(),Locale.getDefault());
字符串结果=”;
试一试{
列出地址=
geoCoder.getFromLocation(LocationManagerHelper.getLatitude()/1E6,
LocationManagerHelper.getLongitude()/1E6,
1);
如果(地址.size()>0){
地址=地址。获取(0);
对于(int i=0;i


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

等等:您定义了一个
地址()
返回
true
的方法-查看您发布的代码块中右大括号前的最后一行-您会发现令人惊讶的是,当您将其结果附加到
TextView
时,显示的文本是
true
?@Giulio Piancatelli,我确实认为返回语句是问题所在。我需要知道o将其切换到.So,等等:您定义了一个
地址()
返回
true
的方法-查看您发布的代码块中右大括号前的最后一行-您会发现令人惊讶的是,当您将其结果附加到
TextView
时,显示的文本是
true
?@Giulio Piancatelli,我确实认为返回语句是问题所在。我需要知道o将其切换为。我不再得到真值,但得到-1?即使它不允许我做
result
,所以我必须将其更改为
result\u OK
,而不是将我的方法返回类型更改为
string
,我必须让它
int
才能工作。int将返回一个数字,因此返回-1。你尝试过str吗使用大写字母S。此外,我将变量的名称更改为result,因此请确保您也更改了代码中的变量声明。只需准确复制我的代码。一旦它工作,请尝试理解原因。您的代码将无法工作,因为
result
必须在
try
块外初始化
return
语句,以执行此操作我不再是真的,但是得到了-1?即使它不允许我做
result
,所以我不得不把它改成
result\u OK
,而不是把我的方法返回类型改成
string
,我必须让它
int
,这样它才能工作。int将返回一个数字,因此是-1。你试过使用cap的字符串吗ital S.另外,我已将变量的名称更改为result,因此请确保您也更改了代码中的变量声明。只需准确复制我的代码。一旦它工作,请尝试了解原因。您的代码将无法工作,因为
result
必须在
try
块之外初始化,以便
return
语句查看。好的,So现在我不知道是真的还是-1,这样就行了。但我仍然无法从经度和纬度得到地址。我猜我需要做一些其他的事情才能得到一个地方的地址?这取决于
LocationManagerHelper中的内容