How to return Latitude and longitude on Android Google-Maps

Asked

Viewed 1,484 times

1

I would like to know how I can return latitude and longitude from an address provided by the User.

Example "Av. Sampaio Vidal,Centro, Marília, SP"

Answer coordenadas -22.225985,-49.94656

2 answers

5

You want to perform an operation of geocoding.

Use the method Geocoder.getFromLocationName(String locationName, int maxResults).

It will return a list of objects Address, which in turn have the methods Address.getLatitude() and Address.getLongitude().

Example

(Untested, it should actually be modified to bring the data into one thread apart, because it is too heavy for the thread home of Android):

Geocoder geocoder = new Geocoder(this);
List<Address> enderecos = geocoder.getFromLocationName("Av. Sampaio Vidal, Centro, Marília, SP", 1);
if (enderecos.size() > 0) {
    Log.v("tag", "coordenadas " + enderecos.get(0).getLatitude() + ", " + enderecos.get(0).getLongitude());
}
  • I’m having a mistake Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 on the line Log.v("tag", "coordenadas " + enderecos.get(0).getLatitude() + ", " + enderecos.get(0).getLongitude());

  • That means the list of objects Address came empty, that is, Google did not find an address near the given coordinates. I changed the response code to include that test.

  • Ops actually meant otherwise, that Google did not find coordinates for the address given.

0

This probably won’t be the answer but it’s just to try to give a light: I get the coordinates by extracting them from a query as follows:

URL PERMANENTE: http://maps.googleapis.com/maps/api/geocode/json?address=

ENDEREÇO: Av. Sampaio Vidal,Centro, Marília, SP

Concatenates the url with the address and will have tudo on the query performed within a array, then simply extract the data you want.

Exemplo: http://maps.googleapis.com/maps/api/geocode/json?address=Av. Sampaio Vidal,Centro, Marília, SP

Success

Browser other questions tagged

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