How to return Latitude and longitude in Android Google-Maps
I would like to know how I can return latitude and longitude from a user-provided address.
Example "Av. Sampaio Vidal,Centro, Marília, SP"
Answer coordenadas -22.225985,-49.94656
2 answers
You want to perform a geocoding operation .
Use the Geocoder method.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 a separate thread as it is too heavy for Android's main thread):
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());
}
This probably won't be the answer but it's just to try to shed some light: I can 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 executed 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