如何使用纬度和经度获得棒棒糖和以上的地址位置? [英] How to get address location in lollipop and above using latitude and longitude?

查看:146
本文介绍了如何使用纬度和经度获得棒棒糖和以上的地址位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与 Geocoder 一起使用纬度找到一个位置的确切地址。经度,但它不支持棒棒糖及以上

I am working with Geocoder to find exact address of a location using latitude and longitude, but it didn't support in lollipop and above

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


推荐答案

您可以在地址中使用以下代码:

You can use following code for Address:

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

或者

Or

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

请参阅以下链接:获取地址

这篇关于如何使用纬度和经度获得棒棒糖和以上的地址位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆