Android 如何使用纬度和经度在棒棒糖和更高版本中获取地址位置?
我正在与Android 如何使用纬度和经度在棒棒糖和更高版本中获取地址位置?,android,google-maps,android-studio,Android,Google Maps,Android Studio,我正在与Geocoder合作,使用纬度和经度查找位置的确切地址,但它不支持棒棒糖及以上版本 Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault()); String mylocation; if (Geocoder.isPresent()) { try { List < Address > addresses = geo.getFromLocation(location.getLatitu
Geocoder
合作,使用纬度和经度查找位置的确切地址,但它不支持棒棒糖
及以上版本
Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
String mylocation;
if (Geocoder.isPresent()) {
try {
List < Address > addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String addressText = String.format("%s, %s, %s",
// If there's a street address, add it
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
// Locality is usually a city
address.getLocality(),
// The country of the address
address.getCountryName());
mylocation = "Lattitude: " + location.getLatitude() + " Longitude: " + location.getLongitude() + "\nAddress: " + addressText;
address1.setText(addressText);
}
} catch (IOException e) {
e.printStackTrace();
}
}
Geocoder geo=new Geocoder(getApplicationContext(),Locale.getDefault());
串定位;
if(Geocoder.isPresent()){
试一试{
Listaddresses=geo.getFromLocation(location.getLatitude(),location.getLatitude(),1);
if(addresses!=null&&addresses.size()>0){
地址=地址。获取(0);
String addressText=String.format(“%s,%s,%s”,
//如果有街道地址,请添加它
address.getMaxAddressLineIndex()>0?address.getAddressLine(0):“”,
//地点通常是一个城市
address.getLocation(),
//地址所在国
address.getCountryName());
mylocation=“latitude:”+location.getLatitude()+“经度:”+location.getLatitude()+“\n地址:”+addressText;
address1.setText(addressText);
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
您可以使用以下代码作为地址:
//Set Address
try {
List < Address > addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0) + " " + addresses.get(0).getAddressLine(1);
if (address != null) {
textViewAddress.setText(address);
} else {
textViewAddress.setText("Not Available");
}
String city = addresses.get(0).getAddressLine(2);
if (city != null) {
textViewCity.setText(city);
} else {
textViewCity.setText("Not Available");
}
String state = addresses.get(0).getAdminArea();
if (state != null) {
textViewState.setText(state);
} else {
textViewState.setText("Not Available");
}
String country = addresses.get(0).getCountryName();
if (country != null) {
textViewCountry.setText(country);
} else {
textViewCountry.setText("Not Available");
}
System.out.println("Address >> " + address + " " + " \n" + state + " \n" + country);
}
} catch (IOException e) {
e.printStackTrace();
}
请参阅以下链接:到目前为止,您尝试了什么?有什么错误信息或代码可以分享吗?我试过了,这两个版本在棒棒糖和更高版本中都不起作用。还有别的办法吗。请help@Jincy:我已经在我的项目中运行了你的代码&它在我的棉花糖设备上运行良好。我正在正确地获取我的lat、lang和地址。你们能定义你们的实际问题吗?问题是我的模拟器,我把它安装在我的真实设备上,然后它就可以了worked@BK:它在棉花糖真实设备上不工作,我在棒棒糖中检查了它,它工作,但棉花糖不工作?完美答案BK19
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL