Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  3.  
  4. dependencies {
  5. //.....
  6. compile 'com.google.android.gms:play-services-location:11.0.1
  7. }
  8.  
  9. import com.google.android.gms.common.ConnectionResult;
  10. import com.google.android.gms.common.api.GoogleApiClient;
  11. importcom.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
  12. import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
  13. import com.google.android.gms.location.LocationListener;
  14. import com.google.android.gms.location.LocationRequest;
  15. import com.google.android.gms.location.LocationServices;
  16. import com.google.android.gms.maps.GoogleMap;
  17.  
  18. public class HomeActivity extends AppCompatActivity implements
  19. ConnectionCallbacks,
  20. OnConnectionFailedListener,
  21. LocationListener {
  22.  
  23. //Define a request code to send to Google Play services
  24. private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
  25. private GoogleApiClient mGoogleApiClient;
  26. private LocationRequest mLocationRequest;
  27. private double currentLatitude;
  28. private double currentLongitude;
  29.  
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_home);
  35.  
  36. mGoogleApiClient = new GoogleApiClient.Builder(this)
  37. // The next two lines tell the new client that “this” current class will handle connection stuff
  38. .addConnectionCallbacks(this)
  39. .addOnConnectionFailedListener(this)
  40. //fourth line adds the LocationServices API endpoint from GooglePlayServices
  41. .addApi(LocationServices.API)
  42. .build();
  43.  
  44. // Create the LocationRequest object
  45. mLocationRequest = LocationRequest.create()
  46. .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
  47. .setInterval(10 * 1000) // 10 seconds, in milliseconds
  48. .setFastestInterval(1 * 1000); // 1 second, in milliseconds
  49.  
  50. }
  51.  
  52. @Override
  53. protected void onResume() {
  54. super.onResume();
  55. //Now lets connect to the API
  56. mGoogleApiClient.connect();
  57. }
  58.  
  59. @Override
  60. protected void onPause() {
  61. super.onPause();
  62. Log.v(this.getClass().getSimpleName(), "onPause()");
  63.  
  64. //Disconnect from API onPause()
  65. if (mGoogleApiClient.isConnected()) {
  66. LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
  67. mGoogleApiClient.disconnect();
  68. }
  69.  
  70.  
  71. }
  72.  
  73. /**
  74. * If connected get lat and long
  75. *
  76. */
  77. @Override
  78. public void onConnected(Bundle bundle) {
  79. Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  80.  
  81. if (location == null) {
  82. LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
  83.  
  84. } else {
  85. //If everything went fine lets get latitude and longitude
  86. currentLatitude = location.getLatitude();
  87. currentLongitude = location.getLongitude();
  88.  
  89. Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "", Toast.LENGTH_LONG).show();
  90. }
  91. }
  92.  
  93.  
  94. @Override
  95. public void onConnectionSuspended(int i) {}
  96.  
  97. @Override
  98. public void onConnectionFailed(ConnectionResult connectionResult) {
  99. /*
  100. * Google Play services can resolve some errors it detects.
  101. * If the error has a resolution, try sending an Intent to
  102. * start a Google Play services activity that can resolve
  103. * error.
  104. */
  105. if (connectionResult.hasResolution()) {
  106. try {
  107. // Start an Activity that tries to resolve the error
  108. connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
  109. /*
  110. * Thrown if Google Play services canceled the original
  111. * PendingIntent
  112. */
  113. } catch (IntentSender.SendIntentException e) {
  114. // Log the error
  115. e.printStackTrace();
  116. }
  117. } else {
  118. /*
  119. * If no resolution is available, display a dialog to the
  120. * user with the error.
  121. */
  122. Log.e("Error", "Location services connection failed with code " + connectionResult.getErrorCode());
  123. }
  124. }
  125.  
  126. /**
  127. * If locationChanges change lat and long
  128. *
  129. *
  130. * @param location
  131. */
  132. @Override
  133. public void onLocationChanged(Location location) {
  134. currentLatitude = location.getLatitude();
  135. currentLongitude = location.getLongitude();
  136.  
  137. Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "", Toast.LENGTH_LONG).show();
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement