您的位置:首页 > 移动开发 > Android开发

在android开发中调用Geocoder方法返回空值的问题

2016-03-21 15:55 465 查看

问题代码如下

//获取对应位置的经纬度并且定位城市
private void updateWithNewLocation(Location location) {
double lat = 0.0,lng = 0.0;
if (location!=null) {
lat = location.getLatitude();
lng = location.getLongitude();
Log.i("TAG", "经度是"+lat+"纬度是"+lng);
System.out.println("程序能走到这一步4");
}else {
cityName = "无法获取城市信息";

}

System.out.println("程序能走到这一步5");
//通过经纬度获取地址
//通过经纬度获取地址
List<Address> list = null;
Geocoder geocoder = new Geocoder(getActivity());
try {
list = geocoder.getFromLocation(lat, lng, 2);
System.out.println("list="+list);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
if (list!=null&&list.size()>0) {
for (int i = 0; i < list.size(); i++) {
Address address = list.get(i);
cityName = address.getLocality();//获取城市
}
}
//发送空消息
handler.sendEmptyMessage(1);
}


logcat中lat和lng的值有正常输出,但是执行完list = geocoder.getFromLocation(lat, lng, 2);之后list依然为空,显示在模拟器上面updateWithNewLocation()这个方法根本没有执行,后来用真机测试这个方法有执行,但是geocoder方法总是返回空值。最后知道是Geocoder()这个方法本身的问题,如果手机没有google service框架总是会返回空值,参看

Pro Android学习笔记(一一五):Location(1):Geocoder
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: