我找到了这个解决方案:here获取位置名称,但我不知道如何在我的textView
中显示它:
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);
Location locations = locationManager.getLastKnownLocation(provider);
List<String> providerList = locationManager.getAllProviders();
if(null!=locations && null!=providerList && providerList.size()>0){
double longitude = locations.getLongitude();
double latitude = locations.getLatitude();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(null!=listAddresses&&listAddresses.size()>0){
String _Location = listAddresses.get(0).getAddressLine(0);
}
} catch (IOException e) {
e.printStackTrace();
t.append("\n " + ????? + " ");
}
}
t是textView
答案 0 :(得分:2)
您在字符串_Location
中有名称地址。因此,您可以在try块中设置TextView
的值。
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(null!=listAddresses&&listAddresses.size()>0){
String _Location = listAddresses.get(0).getAddressLine(0);
}
t.setText(_Location);
}
进一步信息
String address1 = listAddresses.get(0).getAddressLine(0);
String city = listAddresses.get(0).getLocality();
String state = listAddresses.get(0).getAdminArea();
String country = listAddresses.get(0).getCountryName();
String postalCode = listAddresses.get(0).getPostalCode();
String knownName = listAddresses.get(0).getFeatureName();
t.setText(address1 + " " + city + "\n" + state + " " + postalCode);
您可以使用此代码获取有关地址的更多信息