How can I set the address as a street and a neighborhood corresponding to latitude and longitude?

2

I have this code:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == MAPA_REQUEST_BAIRRO){
        if(resultCode == RESULT_OK){
                try {
                Place place = PlacePicker.getPlace(this, data);
                LatLng latLng = place.getLatLng();

                //txtLocalLogradouroB.setText(logradouro +"-"+ bairro);

                Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
                List<Address> addresses1 = null;
                List<Address> addresses = null;

                double um= Double.parseDouble(latitude);
                double dois = Double.parseDouble(longitude);


                addresses1 = gcd.getFromLocation(um, dois, 1);

//                    }
                     if (addresses1.size() > 0) {
                    br = addresses1.get(0).getSubLocality();
                    lg = addresses1.get(0).getThoroughfare();
                    txtLocalLogradouroB.setText(lg + "-"+br);

                }

But it does not work. I have latitude and longitude that comes from a webservice but I want to convert these values to get the address and show to the user could anyone help me please? :)

    
asked by anonymous 26.12.2016 / 19:11

2 answers

5
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
 try {
        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
    } catch (IOException e) {
        e.printStackTrace();
    }

link

    
26.12.2016 / 19:15
2

Option 1:

See a few examples of how you can use Geocoder. But for exactly what you want, try using the function below that returns the location based on LatLng passed:

/**
 * Obtem um endereço baseado LatLng
 * @param latitude e longitude do ponto que quer encontrar
 * @return local em relação ao ponto.
 */
public List<Address> getAddress(LatLng point) {
    try {
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this);
        if (point.latitude != 0 || point.longitude != 0) {
            addresses = geocoder.getFromLocation(point.latitude ,
                    point.longitude, 1);
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);
            System.out.println(address+" - "+city+" - "+country);

            return addresses;

        } else {
            Toast.makeText(this, "latitude and longitude are null",
                    Toast.LENGTH_LONG).show();
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

Option 2:

Pass latitude and longitude to the following query string, and you get a result in JSON:

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

After that ( link ) , just interpret JSON and get only the necessary information.

Details in the documentation Google Maps Geocoding API .

    
26.12.2016 / 19:23