get the current location fast and once in android

前端 未结 7 1598
刺人心
刺人心 2020-12-07 10:48

I have an android application that need device current location (latitude and longitude). I\'ve tried some tutorial on the net and specially some solutions from stack overfl

7条回答
  •  情歌与酒
    2020-12-07 11:45

    What you want to do is achieved using the LocationManager#requestSingleUpdate. This method attaches a listener in a given looper (if you want or have it) and notifies the location ASAP it is received, only once. The method you suggest is used only as an inexact position before the real one is given to you.

    In any case it will be fast than milliseconds (unless you are lucky enough to start listening when a location came to the device). Think on the GPS as an element that you enable when waiting for locations and disable when you remove this listening. This behavior is done to avoid draining the user's battery.

    So, to sum up:

    • The time between you start listening and you receive the position depends on the device's GPS (manufacture, location of the user, satellite coverage...)
    • There is a method in the Android SDK to listen for a single update.
    • By providing the a criteria object, you can manage which criterias are acceptable for you to receive a location. Stronger criterias means more time to get an accurate response.

提交回复
热议问题