Android GoogleMaps myLocation Permission

后端 未结 5 1064
灰色年华
灰色年华 2021-01-17 16:25

With the more recent version of Android... you are supposed to check if the user has given you permission before you use their location info. I\'ve gone through the android

5条回答
  •  一生所求
    2021-01-17 17:02

    I was facing the same error as you. To save battery, i wanted to use the lastKnownLocation instead of polling all the time. Sometimes, lastKnownLocation was null, no matter what i checked before.

    If you have a look in the documentation, it is said that:

    The getLastLocation() method returns a Location object from which you can retrieve the latitude and longitude coordinates of a geographic location. The location object returned may be null in rare cases when the location is not available.

    To fix this, i implemented a combination of using the lastKnownLocation, and polling for a new position as a fallback.

    My basic structure here is

    1. Check if permission is granted
    2. Check if location is enabled
    3. Try to get lastKnownLocation as described in the docs
    4. If null, request a location update in this docs example

    This approach works like a charm. I have'nt managed to figure out a better solution so far, but i would suggest you to try it out.

提交回复
热议问题