Return an address using Google Maps API

Asked

Viewed 4,140 times

1

I’m finishing an application where I need to use the Google Maps API for two things: select an address from the user’s choice (example: user navigates to the desired address and chooses it) and display the map based on the location of a predefined example (example: open the map at the address the user previously chose).

I read that it is possible to receive the selected address using the following code:

private GoogleMap mMap;

{...}

mMap.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng newLatLon) {
        Toast.makeText(getApplicationContext(), point.toString(), Toast.LENGTH_SHORT).show();                       
    }
});

But I would like to pass this function to an external button on the map. How can I do that? And although I looked up how to open a map using a pre-defined address, I couldn’t find anything I could handle. It is possible to pass a parameter of type String for the API?

  • You would like to take the position centered on the map by clicking a button and not inside the Map (There is the method getCameraPosition)? To open the map in a preselected position you need to use the method moveCamera (Setting up a camera update CameraUpdate) or use the method animateCamera (same thing of moveCamera, however it is animated). Both solutions using the instance of GoogleMap.

  • I don’t know if the centered position would be the right one for what I want. I want to get the name of the address of a street that was touched by the user and not its coordinates. I can do this with the getCameraPosition? I’ll look into the moveCamera, thanks for the tip.

  • I understand the problem, you will have to use the API GeoCoder, that does the reverse of the location and provides you with a String with the approximate address of the position. Check out http://developer.android.com/training/location/display-address.html.

1 answer

2


You will have to use a Geolocation Reverse. I did a post about it in 2010 (Android: Reverse Geolocation), but the code for that part hasn’t changed much.

The class containing the data the address is Address. To know each field and what you can get from the address, I suggest you visit documentation. Below the code of a function that searches the city and the line 1 address, which for most cases will return the street name. But be careful that in some cases it may be null, so it is good to do the appropriate tests:

private void getCityByLocation(Location location) {
    //obtendo coordenadas
    double latPoint = location.getLatitude();
    double lngPoint = location.getLongitude();

    //Classe que fornece a localização da cidade
    Geocoder geocoder = new Geocoder(this.getApplicationContext());
    List myLocation = null;

    try {
        //Obtendo os dados do endereço
        myLocation = geocoder.getFromLocation(latPoint, lngPoint, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if ( myLocation != null && myLocation.size() > 0) {
        Address a = myLocation.get(0);
        //Pronto! Vocêm tem o nome da cidade!
        String city = a.getLocality();
        String street = a.getAddressLine(0);
       //Seu código continua aqui...
    } else {
        Log.d("geolocation", "endereço não localizado");
    }
}
  • Thank you very much, that’s just what I needed!

Browser other questions tagged

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