Get current location address for android app

后端 未结 3 1984
长发绾君心
长发绾君心 2020-12-13 00:42

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 s

3条回答
  •  感动是毒
    2020-12-13 01:23

    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.

提交回复
热议问题