Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. public Location getLocation() {
  2. try {
  3. locationManager = (LocationManager) mContext
  4. .getSystemService(LOCATION_SERVICE);
  5.  
  6. // getting GPS status
  7. isGPSEnabled = locationManager
  8. .isProviderEnabled(LocationManager.GPS_PROVIDER);
  9.  
  10. // getting network status
  11. isNetworkEnabled = locationManager
  12. .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  13.  
  14. if (!isGPSEnabled && !isNetworkEnabled) {
  15. // no network provider is enabled
  16. } else {
  17. this.canGetLocation = true;
  18. // First get location from Network Provider
  19. if (isNetworkEnabled) {
  20. locationManager.requestLocationUpdates(
  21. LocationManager.NETWORK_PROVIDER,
  22. MIN_TIME_BW_UPDATES,
  23. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  24. Log.d("Network", "Network");
  25. if (locationManager != null) {
  26. location = locationManager
  27. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  28. if (location != null) {
  29. latitude = location.getLatitude();
  30. longitude = location.getLongitude();
  31. }
  32. }
  33. }
  34. // if GPS Enabled get lat/long using GPS Services
  35. if (isGPSEnabled) {
  36. if (location == null) {
  37. locationManager.requestLocationUpdates(
  38. LocationManager.GPS_PROVIDER,
  39. MIN_TIME_BW_UPDATES,
  40. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  41. Log.d("GPS Enabled", "GPS Enabled");
  42. if (locationManager != null) {
  43. location = locationManager
  44. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  45. if (location != null) {
  46. latitude = location.getLatitude();
  47. longitude = location.getLongitude();
  48. }
  49. }
  50. }
  51. }
  52. }
  53.  
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. }
  57.  
  58. return location;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement