Header Ads

How can I get my location (latitude longitude) using Location Manager

Step 1 : Make a new java class GPSTracker.

Step 2 : Copy the following code in it.
  1. public class GPSTracker extends Service implements LocationListener {
  2. private final Context mContext;
  3. // flag for GPS status
  4. boolean isGPSEnabled = false;
  5. // flag for network status
  6. boolean isNetworkEnabled = false;
  7. // flag for GPS status
  8. boolean canGetLocation = false;
  9. Location location; // location
  10. double latitude; // latitude
  11. double longitude; // longitude
  12. double speed,direction;
  13. // The minimum distance to change Updates in meters
  14. private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
  15. // The minimum time between updates in milliseconds
  16. private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
  17. // Declaring a Location Manager
  18. protected LocationManager locationManager;
  19. public GPSTracker(Context context) {
  20. this.mContext = context;
  21. getLocation();
  22. }
  23. public Location getLocation() {
  24. try {
  25. locationManager = (LocationManager) mContext
  26. .getSystemService(LOCATION_SERVICE);
  27. // getting GPS status
  28. isGPSEnabled = locationManager
  29. .isProviderEnabled(LocationManager.GPS_PROVIDER);
  30. // getting network status
  31. isNetworkEnabled = locationManager
  32. .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  33. if (!isGPSEnabled && !isNetworkEnabled) {
  34. // no network provider is enabled
  35. } else {
  36. this.canGetLocation = true;
  37. // First get location from Network Provider
  38. if (isNetworkEnabled) {
  39. locationManager.requestLocationUpdates(
  40. LocationManager.NETWORK_PROVIDER,
  41. MIN_TIME_BW_UPDATES,
  42. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  43. Log.d("Network", "Network");
  44. if (locationManager != null) {
  45. location = locationManager
  46. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  47. if (location != null) {
  48. latitude = location.getLatitude();
  49. longitude = location.getLongitude();
  50. }
  51. }
  52. }
  53. // if GPS Enabled get lat/long using GPS Services
  54. if (isGPSEnabled) {
  55. if (location == null) {
  56. locationManager.requestLocationUpdates(
  57. LocationManager.GPS_PROVIDER,
  58. MIN_TIME_BW_UPDATES,
  59. MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  60. Log.d("GPS Enabled", "GPS Enabled");
  61. if (locationManager != null) {
  62. Log.d("Getting location", "Location found");
  63. location = locationManager
  64. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  65. if (location != null) {
  66. latitude = location.getLatitude();
  67. longitude = location.getLongitude();
  68. }
  69. }
  70. }
  71. }
  72. }
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. return location;
  77. }
  78. /**
  79. * Stop using GPS listener
  80. * Calling this function will stop using GPS in your app
  81. * */
  82. public void stopUsingGPS(){
  83. if(locationManager != null){
  84. locationManager.removeUpdates(GPSTracker.this);
  85. }
  86. }
  87. /**
  88. * Function to get latitude
  89. * */
  90. public double getLatitude(){
  91. if(location != null){
  92. latitude = location.getLatitude();
  93. }
  94. // return latitude
  95. return latitude;
  96. }
  97. /**
  98. * Function to get longitude
  99. * */
  100. public double getLongitude(){
  101. if(location != null){
  102. longitude = location.getLongitude();
  103. }
  104. // return longitude
  105. return longitude;
  106. }
  107. public double getSpeed(){
  108. return speed;
  109. }
  110. public double getDirection(){
  111. return direction;
  112. }
  113. /**
  114. * Function to check GPS/wifi enabled
  115. * @return boolean
  116. * */
  117. public boolean canGetLocation() {
  118. return this.canGetLocation;
  119. }
  120. /**
  121. * Function to show settings alert dialog
  122. * On pressing Settings button will launch Settings Options
  123. * */
  124. public void showSettingsAlert(){
  125. AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  126. // Setting Dialog Title
  127. alertDialog.setTitle("GPS is settings");
  128. // Setting Dialog Message
  129. alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  130. // On pressing Settings button
  131. alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  132. public void onClick(DialogInterface dialog,int which) {
  133. Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  134. mContext.startActivity(intent);
  135. }
  136. });
  137. // on pressing cancel button
  138. alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  139. public void onClick(DialogInterface dialog, int which) {
  140. dialog.cancel();
  141. }
  142. });
  143. // Showing Alert Message
  144. alertDialog.show();
  145. }
  146. @Override
  147. public void onLocationChanged(Location location) {
  148. if(location != null){
  149. speed = location.getSpeed();
  150. direction = location.getBearing();
  151. }
  152. }
  153. @Override
  154. public void onProviderDisabled(String provider) {
  155. }
  156. @Override
  157. public void onProviderEnabled(String provider) {
  158. }
  159. @Override
  160. public void onStatusChanged(String provider, int status, Bundle extras) {
  161. }
  162. @Override
  163. public IBinder onBind(Intent arg0) {
  164. return null;
  165. }}
________________________________________
Step 3 : Make an object of the GPSTracker class where you need it.
  1. GPSTracker gps = new GPSTracker(this);
Step 4 : Get the Value of the latitude & longitude using the object you created.
  1. double latitude = gps.getLatitude();
  2. double longitude = gps.getLongitude();
Step 5 : Set this location on the Edit Text or Text View ( wherever you want  ).
  1. (EditTextVariable).setText(""+latitude+","+longitude);

No comments:

Powered by Blogger.