How to get the address through Latitude and Longitude, formatted?

0

From a click on the map, I get the latitude and longitude and transform it into an Address String, below the code I use:

private List<android.location.Address> addresses;

    @Override
    public void onMapClick(LatLng latLng) {

        if (mapclickActivate) {
            mappoint = latLng;
            Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
            try {
                addresses = geocoder.getFromLocation(mappoint.latitude, mappoint.longitude, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (mappoint != null) {
                Bundle bundle = new Bundle();
                bundle.putString("address", String.valueOf(addresses));
                Intent sendIntent = new Intent(activity, PointInserirActivity.class);
                sendIntent.putExtras(bundle);
                activity.startActivity(sendIntent);
            }
        }
}

But the address is full of characters, as in this example:

  

[Address [addressLines = [0: "Rua Martins, 132 - Vila Oeste", 1: "Belo   Horizon - MG ", 2:" Brazil "], feature = 132, admin = Mines   General, sub-admin = null, locality = Belo Horizonte, thoroughfare = Rua Pinto   Martins, postalCode = 30532, countryCode = BR, countryName = Brazil, hasLatitude = true, latitude = -19.9398299, hasLongitude = true, longitude = -44.0038374, phone = null, url = null, extras = null]]

How can I get only the street address, number - Neighborhood - Country .. for example?

    
asked by anonymous 23.04.2017 / 23:53

2 answers

1

Question solved as follows:

String street = null;
String city = null;
String adminArea = null; //estado
String country = null;
String address = null;

        addresses = geocoder.getFromLocation(pontoMapeamento.latitude, pontoMapeamento.longitude, 1); 
        street = addresses.get(0).getAddressLine(0);// rua numero e bairro
        city = addresses.get(0).getLocality();//cidade
        country = addresses.get(0).getCountryName();//pais
        adminArea = addresses.get(0).getAdminArea();//estado
        address = street + ", "+city+" - "+adminArea+" - "+country;//endereço da forma desejada
    
24.04.2017 / 01:09
0

Geocoder API callback includes the formatted address ( results.formatted_address of JSON), as can be seen in the following query: link

The documentation of the Address class does not display the getFormattedAddress () method, so an alternative is to query "manually" (just replace the value of the latlng parameter in the URL above) and map the result of the call into a POJO class . I implemented this functionality in a test project that is available on my GitHub. This link shows the method in which I make the call.

    
24.04.2017 / 01:29