Você está na página 1de 5

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

16

Get current location address for android app


android

gps

location

reverse-geocoding

I do not need to display a map. However, I need to use the gps/3g network to locate my current positions ADDRESS (not long and lat) this will
then be added to a automated sms response to inform a person that I currently cant reply, & the include the string address of my current
location. I have the sms stuff working, just need to figure out a method of accessing the gps and pulling an address. I have seen sample code
for lat/long. Perhaps I need to convert lat/long into an address within the google maps API? I am unsure howto go about it. Any advice/code
snippets/similar tutorials welcome! Thanks. :)

share

improve this question


GrumP
363 3 9 35
Matt
8,535 3 31 72

2 Answers

24

Asked
Feb 17 '11 at 12:18

Edited
Apr 29 '13 at 5:12

Order By

There are two steps to this:


1. Get the current location - latitude & longitude, using the GPS, network, last-known location etc. The Android location
documentation includes sample code.
2. Use the Android Geocoder class to request a lookup to convert the lat/long to an Address (from which you can easily extract
city, country, street, etc). Specifically, you need to use the getFromLocation() method

Votes

Note:
The getFromLocation() method returns a SET of matches. In some scenarios you can show the user a set (say 5) and let them
choose the best one, or you can just use the first one, assuming it's best.
Remember that both these calls can take time. The GPS/network may take a while to provide a location. Likewise the call to
getFromLocation() may take time, as it goes over the network to the Google Maps API. Therefore it is critical that you use
extensive error handling for the various scenarios AND that both these calls are moved onto a separate thread so you don't lock
up the app user interface (look into AsyncTask)
The Geocoder class backend is only present on Google-approved devices. So the lookup will basically fail on any device that
doesn't have GMail/Market/Maps on it.

share

improve this answer


Ollie C
16.2k 23 96 162

Answered
Feb 17 '11 at 12:44

Edited
Feb 18 '11 at 11:38

Thanks. :) Do you know of any tutorials that would make it easier? I'm finding it a little hard to follow. :-\ GrumP Feb 17 '11 at 18:48

Unfortunately, it is quite complicated. I'd suggest you first write code to get the location, then to do the geocode, and then after that start using asynctask if you want
better performance. The code in the link above is the best place to start as it fully explains everything. It's just a matter of spending time reading, coding, re-reading,
debugging, until you understand it. Ollie C Feb 17 '11 at 18:55
Thanks Ollie. :) GrumP Feb 17 '11 at 20:10

Ollie, can get the lat/long working now. Will work on trying to convert it to an actual address next. Thanks Again. :) GrumP Feb 17 '11 at 21:51

Read up on AsyncTask, it makes it pretty easy. Easiest way is to create an inner class inside your activity (subclass AsyncTask) and move your code in there.
doInBackground() is where you put the name lookup call, onPostExecute() is where you update your UI. developer.android.com/reference/android/os/AsyncTask.html
Ollie C Feb 22 '11 at 16:10
show 2 more comments

11

The answer is here: http://developer.android.com/reference/android/location/Geocoder.html


You need to create Geocoder object and call getFromLocation(double latitude, double longitude, int maxResults)
Geocoder gCoder = new Geocoder(myContext);
ArrayList<Address> addresses = gCoder.getFromLocation(123456789, 123456789, 1);
if (addresses != null && addresses.size() > 0) {
Toast.makeText(myContext, "country: " + addresses.get(0).getCountryName(), Toast.LENGTH_LONG).show();
}

share

improve this answer


WarrenFaith
40.1k 19 106 124

Answered
Feb 17 '11 at 12:43

Edited
Nov 4 '13 at 13:16

Thanks, find it all a tad confusing but will have a good read and see if I get anywhere :) GrumP Feb 17 '11 at 18:49
Really struggling with this. I can get the lat and long and print it to the screen. I want to print the address instead. Can anyone show me howto create the geocoder
object and howto impliment the getFromLocation method? Read the provided link, but totally lost atm. GrumP Feb 21 '11 at 0:14
I updated my answer... WarrenFaith Feb 21 '11 at 1:02
Thanks Warren. Really thanks for your help. :) I'm not sure what should be where "myContext" is. "Length cannot be resolved or is not a field" is an error too. My lat and
long have been added to a string called my coords, which is retrieved from another class with a get method. This complicates it further. GrumP Feb 21 '11 at 12:46

If you use this inside an activity, you can use "this" as the context, because the activity inherits from the context. Otherwise try to find a corresponding context
somewhere. The length was a mistake and is only supported by pure arrays. should be size(). WarrenFaith Feb 21 '11 at 18:37
show 1 more comment

Your Answer

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Você também pode gostar