【发布时间】: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函数