我想从纬度和经度在安卓得到以下值

街道地址 城市/州 邮政编码 完整的地址

如何做到这一点?


当前回答

试试这个,我的朋友

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

其他回答

如果您使用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
}

但是当你调用这个方法时,这个方法只返回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"
    }
}

从latlong (Geo-coordinates)获取Address还有最后一个技巧。你可以简单地通过经度和纬度点击谷歌地图web服务。它只是一个GET-Method web服务。

它将返回JSON响应,可以很容易地解析以获得地址。它的URL是:

http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true

你可以用lat,long代替32,75。

使用Geocoder类从纬度和经度中获得完整地址非常容易。下面是代码示例。希望这能有所帮助!

 if (l != null) {
        val lat = l.latitude
        val lon = l.longitude

        val geocoder = Geocoder(this, Locale.getDefault())
        val addresses: List<Address>

        addresses = geocoder.getFromLocation(lat, lon, 1) 

        val address = addresses[0].getAddressLine(0)
        val address2 = addresses[0].getAddressLine(1)
        val city = addresses[0].locality
        val state = addresses[0].adminArea
        val country = addresses[0].countryName
        val postalCode = addresses[0].postalCode
        val knownName = addresses[0].featureName

        val message =
                "Emergency situation. Call for help. My location is: " + address + "." + "http://maps.google.com/maps?saddr=" + lat + "," + lon

    }

您可以只使用地址值,因为它提供了所有完整的地址。如果需要单独的组件,也可以使用其他组件。

使用地理编码器,你可以得到这样的东西!

           try {
                Geocoder geo = new Geocoder(MapsActivity.this.getApplicationContext(), Locale.getDefault());
                List<Address> addresses = geo.getFromLocation(origin.latitude, origin.longitude, 1);
                address.setText("Loading...");
                if (addresses != null && addresses.size() > 0) {
                    String locality = addresses.get(0).getAddressLine(0);
                    String country = addresses.get(0).getCountryName();
                    String state = addresses.get(0).getAdminArea();
                    String sub_admin = addresses.get(0).getSubAdminArea();
                    String city = addresses.get(0).getFeatureName();
                    String pincode = addresses.get(0).getPostalCode();
                    String locality_city = addresses.get(0).getLocality();
                    String sub_localoty = addresses.get(0).getSubLocality();
                    if (locality != null && country != null) {
                        address.setText(locality + ", " + (sub_localoty != null ? sub_localoty + ", " : "")  + (locality_city != null ? locality_city + ", " : "" ) + (city != null ? city + ", " : "")  + (sub_admin != null ? sub_admin + ", " : "") + (state != null ? state + ", " : "") + country + ", " + (pincode != null ? pincode : ""));
                    } else {
                        address.setText("Location could not be fetched...");
                    }
                }
            } catch (Exception e) {
                address.setText("Location could not be fetched...");
                e.printStackTrace(); // getFromLocation() may sometimes fail
            }

只要用这个方法,把你的背,长。

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