【问题标题】:unable to get both latitude and longitude from address, I get one of them either latitude or longitude无法从地址中获取纬度和经度,我得到其中一个纬度或经度
【发布时间】:2016-06-22 09:22:56
【问题描述】:

我正在开发一个应用程序,该应用程序可以在用户输入地址后使用纬度和经度。我正在使用地理编码器从输入地址获取纬度和经度。但问题是两者都从地理编码器返回相关地址,但我只能读取其中一个。以下是我正在使用的代码:

        Geocoder geocoder = new Geocoder(this);
    List<Address> addresses;
    double[] cordinates = new double[2];
    try {
        addresses = geocoder.getFromLocationName(locName, 1);
        if (addresses.size() > 0) {
            cordinates[0] = addresses.get(0).getLatitude();
            cordinates[1] = addresses.get(0).getLongitude();//unable to get this one 


            return cordinates;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
        return null;
}

在上述情况下,我能够获得纬度但无法获得经度。我在调试后知道这一点。现在当我像这样交换它们时

                 cordinates[1] = addresses.get(0).getLongitude();                
                 cordinates[0] = addresses.get(0).getLatitude();

现在我得到经度但不是纬度。 这里有什么问题?

【问题讨论】:

  • 在您的问题中包含异常堆栈跟踪
  • @MaxZavernutiy 代码跳转到方法的最后一个,读取第二个坐标时返回null。没有产生异常堆栈
  • 尝试将Address 分配给局部变量Address address = addresses.get(0),然后改用局部变量。当您调用 get 时,List 似乎正在使用 Address。不知道为什么会发生这种情况(如果发生的话)。

标签: android performance android-studio


【解决方案1】:

这工作正常。由于您在遇到
return cordinates; 时使用断点调试代码,因此它会转到 return null; 它仅在调试时发生。它总是返回正确的值。您可以通过插入日志语句来检查返回值。

 {
     .....
     double[] coordinates = getLongLat("Your address");
     Log.wtf(TAG,"Lat:"+coordinates[0]+" Long:"+coordinates[1]);// This will log the correct values
     .....
 }


 public double[]  getLongLat(String address){
    Geocoder geocoder = new Geocoder(this);
    List<Address> addresses;
    double[] cordinates = new double[2];

   try {
       addresses = geocoder.getFromLocationName(address, 1);
       if (addresses.size() > 0) {
          Address address1 = addresses.get(0);
          cordinates[0] = address1.getLatitude();
          cordinates[1]  = address1.getLongitude();
          return cordinates;
    }

    } catch (IOException e) {
        e.printStackTrace();
    }
       return null;
}

【讨论】:

  • 是的,它工作正常,感谢您提供的信息。但是我还是不知道为什么debug语句会跳过第一个return语句而返回null?
  • @SayyedErfanian 它不会跳过第一个返回语句。当它遇到 return 语句时,它会转到代码块的末尾。你也接受了错误的答案。你能纠正一下吗?
【解决方案2】:

试试这个工作代码:

AutoCompleteTextView acGooglePlaces = (AutoCompleteTextView) findViewById(R.id.ac_edit_my_event_places);

    acGooglePlaces.setAdapter(new GooglePlacesAutocompleteAdapter(GooglePlaces.this, R.layout.auto_complete_text_layout));
    acGooglePlaces.requestFocus();

    btnSearch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            try {
                List<Address> returnedaddresses = geoCoder.getFromLocationName(acGooglePlaces.getText().toString(),1);

                if(!returnedaddresses.isEmpty()){

                    String latForVol = String.valueOf(returnedaddresses.get(0).getLatitude());
                    String longForVol = String.valueOf(returnedaddresses.get(0).getLongitude());

                    Log.e("Lat", latForVol);
                    Log.e("Long", longForVol);
                    Log.e("Location", acGooglePlaces.getText().toString());

                }else {
                    Log.e("Check", "Please give the correct address");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });

【讨论】:

  • @SayyedErfanian 你有纬度或经度吗?否则你会进入 else 循环?
  • 解决了.....错误是跳过了if中的return语句,而是执行了end的return null。最后一个 return 语句放置在错误的位置。我忘了把它放在 else 语句中。是的,长时间空腹工作让我很受打击。谢谢你的回答。
【解决方案3】:
public static LatLng reverseGeocoding(Context context, String locationName){
    if(!Geocoder.isPresent()){
        Log.w("zebia", "Geocoder implementation not present !");
    }
    Geocoder geoCoder = new Geocoder(context, Locale.getDefault());

    try {
        List<Address> addresses = geoCoder.getFromLocationName(locationName, 1);



    } catch (IOException e) {
        Log.d(Geocoding.class.getName(), "not possible finding LatLng for Address : " + locationName);
    }

    if(addresses.size() > 0){
            Log.d("zebia", "reverse Geocoding : locationName " + locationName + "Latitude " + addresses.get(0).getLatitude() );
            return new LatLng(addresses.get(0).getLatitude(), addresses.get(0).getLongitude());
    }else{
            //use http api
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多