在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_L