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

Asked

Viewed 1,312 times

0

From a click on the map, I take latitude and longitude and turn it into a String address, 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 comes full of characters, as in this example:

[Address[addressLines=[0:"Rua Martins, 132 - Vila Oeste",1:"Belo Horizonte - MG",2:"Brasil"],Feature=132,admin=Minas General,sub-admin=null,locality=Belo Horizonte,Horoughfare=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 address Street number - neighborhood - country .. for example?

3 answers

2


Question resolved 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

0

Only by complementing the response of Henrique Mendes himself, the variable "Addresses" should be declared as a List of Address. As demonstrated below and explained in geocoder reference.

List<Address> addresses = geocoder.getFromLocation(pontoMapeamento.latitude, pontoMapeamento.longitude, 1);

0

Return of Geocoder API calls includes formatted address (results.formatted_address JSON), as seen in the following query: http://maps.googleapis.com/maps/api/geocode/json?latlng=-19.9398299,-44.0038374

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.