如何在android中获取位置的名称

时间:2014-01-08 08:32:18

标签: java android

假设我有某个位置的Long和Lat。我有可能检索到该位置的名称吗?比如喷气式披萨或沃尔玛......

3 个答案:

答案 0 :(得分:4)

这是完整的位置方法。 只需从任何地方拨打电话。 您将获得用户的当前位置。完整的街道,城市和国家地址

您也可以将纬度和经度值传递给。 addresses = gcd.getFromLocation(latitude, longitude, 1);获取任何特定值的位置。

public String getUserLocation() {

    String address = "";

    LocationManager locManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);

    boolean network_enabled = locManager
            .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    Location location;

    if (network_enabled) {

        location = locManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null) {
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();

            Log.e("longitude  = ", longitude + "lat");
            Log.e("latitude  = ", latitude + "long");

            Geocoder gcd = new Geocoder(context, Locale.getDefault());
            List<Address> addresses = null;
            try {
                addresses = gcd.getFromLocation(latitude, longitude, 1);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return address;
            }
            if (addresses.size() > 0) {

                String addressline = addresses.get(0).getAddressLine(0);
                String city = addresses.get(0).getAddressLine(1);
                String country = addresses.get(0).getAddressLine(2);

                Log.e("Address", addressline + ", " + city + ", " + country);
                return address = addressline + ", " + city + ", " + country;
            }

            return address;
        }
    }

    return address;

}

希望这有帮助:)

答案 1 :(得分:1)

The Geocoder这样做。但它使用的是互联网,并不像你一直那样准确,但是它可以胜任。

答案 2 :(得分:1)

在Address对象中,你有很多有趣的东西:

http://developer.android.com/reference/android/location/Address.html

例如getFeatureName()

从纬度/经度坐标中检索地址:

Geocoder geoCoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1); // the number of result you want, in your case probably just one.