如何从纬度和经度获得完整的地址?

UMA*_*MAR 191 android location google-maps

我想从android中获取Latitude和Longitude中的以下值

  1. 街道地址
  2. 市,州
  3. 压缩
  4. 完整地址

怎么做到这一点?

use*_*305 470

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());

addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

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
Run Code Online (Sandbox Code Playgroud)

有关可用详细信息的更多信息,请查看Android-Location-Address

  • "getAddressLine()"对于获取城市/国家是不可靠的,因为地址线可能因地理编码细节的级别而异.请改用`getLocality()`和`getCountryName()`. (9认同)
  • @Shubh - 试试这个网址 - "http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude +","+ longitude +"&sensor = true"`.它将返回Json响应. (9认同)
  • 地理编码器查找可能需要很长时间.把它称为一个单独的线程要好得多.与[Google Doc示例]中一样(https://developer.android.com/training/location/display-address.html) (8认同)

小智 62

试试这个我的朋友

 private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
            String strAdd = "";
            Geocoder geocoder = new Geocoder(this, Locale.getDefault());
            try {
                List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
                if (addresses != null) {
                    Address returnedAddress = addresses.get(0);
                    StringBuilder strReturnedAddress = new StringBuilder("");

                    for (int i = 0; i <= returnedAddress.getMaxAddressLineIndex(); i++) {
                        strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                    }
                    strAdd = strReturnedAddress.toString();
                    Log.w("My Current loction address", strReturnedAddress.toString());
                } else {
                    Log.w("My Current loction address", "No Address returned!");
                }
            } catch (Exception e) {
                e.printStackTrace();
                Log.w("My Current loction address", "Canont get Address!");
            }
            return strAdd;
        }
Run Code Online (Sandbox Code Playgroud)


chi*_*ada 24

城市国家并不总是进入地铁1号线和2号线......

示例在这里

所以,

Geocoder geocoder = new Geocoder(context, Locale.getDefault());

List<Address> addresses  = geocoder.getFromLocation(latitude,longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String zip = addresses.get(0).getPostalCode();
String country = addresses.get(0).getCountryName();
Run Code Online (Sandbox Code Playgroud)


Rah*_*ina 24

从Lat-Long(地理坐标)获取地址是最后一招.您可以直接点击经过纬度和经度的谷歌地图网络服务.它只是一个GET-Method网络服务.

它将返回可以轻松解析以获取地址的JSON响应.这个URL是:

http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true
Run Code Online (Sandbox Code Playgroud)

您可以 lat,long 替换32,75.

  • 现在已弃用。 (3认同)
  • 您现在无法在未经身份验证的情况下请求该 API (2认同)

小智 11

如果你使用Kotlin语言,我创建这个方法来直接获取地址位置

private fun getAddress(latLng: LatLng): String {
    val geocoder = Geocoder(this, Locale.getDefault())
    val addresses: List<Address>?
    val address: Address?
    var addressText = ""

    addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1)

    if (addresses.isNotEmpty()) {
        address = addresses[0]
        addressText = address.getAddressLine(0)
    } else{
        addressText = "its not appear"
    }
    return addressText
}
Run Code Online (Sandbox Code Playgroud)

但是当你调用这个方法时,这个方法只是返回 String 值

如果你想获得所有地址,你只需使用这个方法/函数

fun getAddress(latLng: LatLng){
    val geocoder = Geocoder(this, Locale.getDefault())
    val addresses: List<Address>?
    val address: Address?
    var fulladdress = ""
    addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1)

    if (addresses.isNotEmpty()) {
        address = addresses[0]
        fulladdress = address.getAddressLine(0) // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex
        var city = address.getLocality();
        var state = address.getAdminArea();
        var country = address.getCountryName();
        var postalCode = address.getPostalCode();
        var knownName = address.getFeatureName(); // Only if available else return NULL
    } else{
        fulladdress = "Location not found"
    }
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*ode 8

在onCreate()..

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);




    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(bestProvider);

    if (location == null) {
        Toast.makeText(getApplicationContext(), "GPS signal not found",
                3000).show();
    }
    if (location != null) {
        Log.e("locatin", "location--" + location);

        Log.e("latitude at beginning",
                "@@@@@@@@@@@@@@@" + location.getLatitude());
        onLocationChanged(location);
    }
Run Code Online (Sandbox Code Playgroud)

在onLocationChanged()中编写代码

@Override
public void onLocationChanged(Location location) {

    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(this, Locale.getDefault());

    latitude = location.getLatitude();
    longitude = location.getLongitude();

    Log.e("latitude", "latitude--" + latitude);

    try {
        Log.e("latitude", "inside latitude--" + latitude);
        addresses = geocoder.getFromLocation(latitude, longitude, 1);





        if (addresses != null && addresses.size() > 0) {
            String address = addresses.get(0).getAddressLine(0); 
            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(); 

            locationTxt.setText(address + " " + city + " " + country);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)


Mah*_*eTo 6

您正在寻找术语地理编码。

简短的故事是你需要做的:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Run Code Online (Sandbox Code Playgroud)

要执行更多操作,您应该在此处阅读 Geocoder 。


Ham*_*han 5

只需使用此方法并传递您的经纬度即可。

public static void getAddress(Context context, double LATITUDE, double LONGITUDE{
    //Set Address
    try {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

        if (addresses != null && addresses.size() > 0) {
            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
            Log.d(TAG, "getAddress:  address" + address);
            Log.d(TAG, "getAddress:  city" + city);
            Log.d(TAG, "getAddress:  state" + state);
            Log.d(TAG, "getAddress:  postalCode" + postalCode);
            Log.d(TAG, "getAddress:  knownName" + knownName);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return;
}
Run Code Online (Sandbox Code Playgroud)


Gau*_*ani 5

  public static String getAddressFromLatLng(Context context, LatLng latLng) {
    Geocoder geocoder;
    List<Address> addresses;
    geocoder = new Geocoder(context, Locale.getDefault());
    try {
        addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
        return addresses.get(0).getAddressLine(0);
    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}
Run Code Online (Sandbox Code Playgroud)