Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current location address for android app

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. :)

like image 338
GrumP Avatar asked Feb 17 '11 12:02

GrumP


People also ask

How do I find the full address of my current location?

With the help of latitude and longitude, you can get city name and address. To get full street name, use getMaxAddressLineIndex(). In onLocationChanged, check whether you are getting current location or not. @T.

How can I get my current location in Android?

There are two ways to get the current location of any Android device: Android's Location Manager API. Fused Location Provider: Google Play Services Location APIs.

How can I get current location in location manager?

First, create a Location Manager object by calling the getSystemService() method and LOCATION_SERVICE as an argument. Call getBestProvider() to get the location in the form of a string. Now the provider to getLastKnownLocation() as an argument to get the location.

How can get current latitude and longitude in Android programmatically?

Steps to get current latitude and longitude in Android Location permissions for the manifest file for receiving the location update. Create a LocationManager instance as a reference to the location service. Request location from LocationManager. Receive location update from LocationListener on change of location.


1 Answers

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

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.
like image 132
Ollie C Avatar answered Oct 19 '22 04:10

Ollie C