【问题标题】:How to get address location in lollipop and above using latitude and longitude?如何使用纬度和经度获取棒棒糖及以上的地址位置?
【发布时间】:2017-01-25 10:44:23
【问题描述】:

我正在与Geocoder 合作使用latitudelongitude 查找位置的确切地址,但它不支持lollipop 及更高版本

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();
 }
}

【问题讨论】:

  • 到目前为止您尝试过什么?有什么错误信息或代码可以分享吗?

标签: android google-maps android-studio


【解决方案1】:

地址可以使用以下代码:

//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();
 }

或者

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

参考以下链接:Get Address

【讨论】:

  • 我试过了,两者都不能在棒棒糖及以上版本中工作。有没有其他办法。请帮忙
  • @Jincy:我已经在我的项目中运行了你的代码,并且它在我的棉花糖设备上运行良好。我得到了我的纬度、语言和地址。你能定义你的实际问题吗?
  • 问题出在我的模拟器上,我把它安装在我的真实设备上,然后它就可以工作了
  • @BK:它在棉花糖真实设备上不起作用,我在棒棒糖中检查了它,它工作但棉花糖不行?
  • 完美答案BK19
猜你喜欢
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
相关资源
最近更新 更多