【问题标题】:Error in getting address of a location in android在android中获取位置地址时出错
【发布时间】:2014-07-06 11:40:14
【问题描述】:

基于本教程:http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/。我现在可以根据位置获取纬度和经度。现在我',尝试使用 Geocoder 获取确切地址:(主要是上面所属的类代码)

GPSTracker GPS = new GPSTracker(Main.this);

double latitude = GPS.getLatitude();
double longitude = GPS.getLongitude();

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(Main.this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);

LogCat 说:

07-06 11:29:41.911: W/System.err(1670): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

我想知道我做错了什么?

【问题讨论】:

  • addresses的大小等于0,所以你不能得到第一个索引
  • 这是否意味着地理编码器无法从位置填充列表,或者列表声明错误?
  • 我认为第一个想法是正确的,在获取列表的地址检查大小之前,如果大于 0 则获取您的数据,
  • 防止出错很好,但是由于列表的大小仍然是0,我仍然没有得到地址字符串
  • 使用 google API (developers.google.com/maps/documentation/geocoding/?csw=1) 检查 getFromLocation 函数

标签: android gps location


【解决方案1】:
GPSTracker GPS = new GPSTracker(Main.this);

double latitude = GPS.getLatitude();
double longitude = GPS.getLongitude();

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(Main.this, Locale.getDefault());

addresses = geocoder.getFromLocation(latitude, longitude, 1);
if(addresses!=null && addresses.size()!=0){

    String address = addresses.get(0).getAddressLine(0);

}

//if(addresses!=null &&addresses.size()!=0)...检查这部分..您的地址不能为空,并且数组的大小不应为零...

【讨论】:

  • if(addresses!=null &&addresses.size()!=0)...检查这部分..您的地址不能为空,并且数组的大小不应为零.. .
猜你喜欢
  • 2011-01-06
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 2019-12-22
  • 2015-06-23
  • 1970-01-01
  • 2011-06-29
相关资源
最近更新 更多