在Activity的方法onCreate里写
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(act, R.layout.main);
Location testLocation = new Location("");
testLocation.setLongitude(125.125621);
testLocation.setLatitude(46.636818);
getAddress(testLocation);
}
调用方法
//获取地址信息:城市、街道等信息
private List<Address> getAddress(Location location) {
List<Address> result = null;
try {
if (location != null) {
Geocoder gc = new Geocoder(this, Locale.getDefault());
result = gc.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
Toast.makeText(this, "获取地址信息:"+result.toString(), Toast.LENGTH_LONG).show();
Log.v("TAG", "获取地址信息:"+result.toString());
for(int i=0;i<result.size();i++){
Log.i("ceshi", "position:"+i+",省:"+result.get(i).getAdminArea()+",市:"+result.get(i).getLocality());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
获取结果:
|