How do I get the current GPS location programmatically in Android?

前端 未结 22 2712
梦谈多话
梦谈多话 2020-11-21 04:42

I need to get my current location using GPS programmatically. How can i achieve it?

22条回答
  •  春和景丽
    2020-11-21 04:49

    Getting location updates requires lots of bolierplate code in Android, You need to take care of

    • Google Play services availablity Check,
    • Update Google play Service if it is old or not available
    • Dialog Creation of GoogleApiClient and its callbacks connected,disconnected etc.
    • Stopping and releasing resources for location updates
    • Handling Location permission scenarios
    • Checking Location services are On or Off
    • Getting lastknown location is not so easy either
    • Fallback to last known location if not getting location after certain duration

    To ease out all these steps i have created Android-EasyLocation (small android library) which will take care all this stuff and you can focus on business logic.

    All you need is extend EasyLocationActivity and this

    requestSingleLocationFix(easyLocationRequest);
    

    or

    requestLocationUpdates(easyLocationRequest);
    

    Checkout sample app and steps needed here at https://github.com/akhgupta/Android-EasyLocation

提交回复
热议问题