GPS定位的问题,location对象为空

知道分子 知道分子 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location == null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}

Log.i(TAG, "location= " + location +" locationManager= " + locationManager);
String addressString = null;
double latitude=location.getLatitude();
double longitude=location.getLongitude();
Geocoder gc=new Geocoder(this,Locale.getDefault());
try
{
List<Address> addresses=gc.getFromLocation(latitude, longitude,1);
StringBuilder sb=new StringBuilder();

Log.i(TAG, "addresses= " + addresses);

if(addresses.size()>0)
{
Address address=addresses.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++)
sb.append(address.getAddressLine(i)).append("\n");

sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
addressString=sb.toString();
Log.i(TAG, "latitude= " + latitude +" longitude= " + longitude + " city= " + addressString);
}
}
catch(IOException e)


1. 为什么获得的location对象都是null啊?
2. 把经纬度写死后,用Geocoder解析时,返回的是一个 空list。

哪位大侠能解释一下,权限也设置了,不知道问题出在哪,求解答。
...全文
评论 7
收藏 已收藏
分享
返回顶部
知道分子
Android 80,490 成员 91,371 内容