Geocoder无法从android中的特定字符串获取lat lng?

时间:2017-02-24 11:26:10

标签: android geocoding google-geocoder

我正在尝试为以下地址获取lat lng但地理编码器无法找到lat lng:

  

Cascina Palazzetto n.9 - Strada Provinciale 134,POIRINO(TO) - 10046,Italia

我的代码段如下:

public static LatLng getLocationFromAddress(Context context, String strAddress) {
  Geocoder coder = new Geocoder(context);
  List<Address> address;
  LatLng p1 = null;
  try {
       address = coder.getFromLocationName(strAddress, 5);
       if (address == null) {
           return null;
       }
       Address location = address.get(0);
       location.getLatitude();
       location.getLongitude();
     p1 = new LatLng(location.getLatitude(), location.getLongitude());
  } catch (Exception ex) {
       Timber.d("getLocationFromAddress(printStackTrace): " + ex);
  }
  return p1;
}

编辑1:

此外,我尝试在地理编码器上手动设置意大利语的语言环境,但我仍然无法获得结果。

1 个答案:

答案 0 :(得分:-1)

尝试运行地址列表循环,当你得到lat,长休息循环。

像,

  public static LatLng getLocationFromAddress(Context context, String strAddress) {
      Geocoder coder = new Geocoder(context);
      List<Address> address;
      LatLng p1 = null;
      try {
           address = coder.getFromLocationName(strAddress, 5);
           if (address == null) {
               return null;
           }

           for(int i=0;i<address.size();i++)
           {


           Address location = address.get(i);

           if(location.getLatitide!=0 && location.getLongitude!=0)
           {
                      location.getLatitude();
                      location.getLongitude();
                      p1 = new LatLng(location.getLatitude(),                      location.getLongitude());

                    break;
           }
       }
      } catch (Exception ex) {
           Timber.d("getLocationFromAddress(printStackTrace): " + ex);
      }
      return p1;
    }