天天看點

android 擷取定位為空,Android使用百度定位API時擷取的位址資訊為null

今天在使用百度定位api做開發時,發現調用LocationClient定位後,在LocationListener的回調方法裡,擷取的位址資訊為null,代碼如下:

public class MyLocationListener implements BDLocationListener {

@Override

public void onReceiveLocation(BDLocation location) {

pd.dismiss();

if(location == null){

Toast.makeText(MainActivity.this, "無法定位", Toast.LENGTH_SHORT).show();

return ;

}

double longitude = location.getLongitude();

double latitude = location.getLatitude();

String address = location.getAddrStr(); //這裡擷取詳細位址,取到的是null

longitudeTv.setText(longitude + "");

latitudeTv.setText(latitude + "");

addressTv.setText(address);

}

}查了下資料發現,是沒有配置相關參數,應該在初始化時加上如下代碼:

locationClient = new LocationClient(this);

LocationClientOption option = locationClient.getLocOption();

option.setAddrType("all"); //加上這個配置後才可以取到詳細位址資訊加上上面的配置後,再通過location.getAddrStr()方法擷取位址,就不會為null了