1

I have calculated the latitude and longitude of current place. What I want now is to get the location of that place. here is my java code to calculate longitude and latitude.I have taken the permission in manifest code,that i am not including with this java code.

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            textView.append("\n " + location.getAltitude() + " " + location.getLongitude());
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.INTERNET
        }, 10);
        return;
    } else {
        configureButton();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 10:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                configureButton();
            return;
    }
}

private void configureButton() {
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            locationManager.requestLocationUpdates("gps", 5000, 0, locationListener);
        }
    });
}
Pro Mode
  • 1,453
  • 17
  • 30
r_ranjan
  • 189
  • 1
  • 2
  • 5
  • 2
    Please specify what do you mean under "location of that place"? Latitude and longitude are already a location of the place. Do you want to construct instance of Location class with specified coordinates? – Alexey Guseynov Sep 28 '16 at 06:44

2 Answers2

1
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
String result = null;
try {
        List<Address> addressList = geocoder.getFromLocation(
        latitude, longitude, 1); //use your lat, long value here

        if (addressList != null && addressList.size() > 0) {
            Address address = addressList.get(0);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
                sb.append(address.getAddressLine(i)).append("\n");
            }
            sb.append(address.getLocality()).append("\n");
            sb.append(address.getPostalCode()).append("\n");
            sb.append(address.getCountryName());
            result = sb.toString();
            }
    } catch (IOException e) {
        Log.e(TAG, "Unable connect to Geocoder", e);
}

More reference:

Community
  • 1
  • 1
Sam
  • 51
  • 5
0

Here is my Code snippet I used to get location from Latitude and longitude.

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

Or you can use Official documentation to get more custom information from this Link: https://developer.android.com/training/location/display-address.html

Here is Sample code for the same:

http://www.androidauthority.com/get-location-address-android-app-628764/

Er. Kaushik Kajavadara
  • 1,657
  • 2
  • 16
  • 37