简体   繁体   中英

How to get location's name in android

Lets say I have the Long and Lat of a certain location. Is it possible I can retrieve the name of the location? like jet's pizza or walmart...

here is the full method of location. Just call this from any where. You will get the User's current location. with full street, city and country address

also you can pass latitude and longitude values to . addresses = gcd.getFromLocation(latitude, longitude, 1); to get location of any particular values.

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;

}

Hope this help :)

The Geocoder does that. It uses Internet, though, and is not as accurate as you'd like always, but it does the job.

In the Address object you have a lot of interesting stuff :

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

like getFeatureName() for exemple.

To retrieve addresses from a lat/long coordinates :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM