Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. package com.example.aminepc.testappgooglemaps;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.icu.text.DateFormat;
  6. import android.location.Location;
  7. import android.support.v4.app.ActivityCompat;
  8. import android.support.v4.app.FragmentActivity;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11.  
  12. import com.google.android.gms.common.ConnectionResult;
  13. import com.google.android.gms.common.api.GoogleApiClient;
  14. import com.google.android.gms.location.FusedLocationProviderApi;
  15. import com.google.android.gms.location.LocationListener;
  16. import com.google.android.gms.location.LocationRequest;
  17. import com.google.android.gms.location.LocationServices;
  18. import com.google.android.gms.maps.CameraUpdateFactory;
  19. import com.google.android.gms.maps.GoogleMap;
  20. import com.google.android.gms.maps.OnMapReadyCallback;
  21. import com.google.android.gms.maps.SupportMapFragment;
  22. import com.google.android.gms.maps.model.LatLng;
  23. import com.google.android.gms.maps.model.MarkerOptions;
  24.  
  25. import java.util.Date;
  26.  
  27.  
  28. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
  29. GoogleApiClient.ConnectionCallbacks,
  30. GoogleApiClient.OnConnectionFailedListener,
  31. LocationListener{
  32.  
  33. TextView latitudeText;
  34. TextView longitudeText;
  35. private FusedLocationProviderApi locationProvider = LocationServices.FusedLocationApi;
  36. private GoogleApiClient googleApiClient;
  37. private LocationRequest locationRequest;
  38. private Double myLatitude;
  39. private Double myLongitude;
  40. GoogleMap mMap;
  41.  
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. // Retrieve the content view that renders the map.
  47. setContentView(R.layout.activity_maps);
  48. // Get the SupportMapFragment and request notification
  49. // when the map is ready to be used.
  50. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  51. .findFragmentById(R.id.map);
  52. mapFragment.getMapAsync(this);
  53.  
  54. latitudeText = (TextView) findViewById(R.id.tvLatitude);
  55. longitudeText = (TextView) findViewById(R.id.tvLongitude);
  56.  
  57. googleApiClient = new GoogleApiClient.Builder(this)
  58. .addApi(LocationServices.API)
  59. .addConnectionCallbacks(this)
  60. .addOnConnectionFailedListener(this)
  61. .build();
  62.  
  63. locationRequest = new LocationRequest();
  64. locationRequest.setInterval(10 * 1000);
  65. locationRequest.setFastestInterval(15 * 1000);
  66. locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  67.  
  68.  
  69. }
  70.  
  71.  
  72.  
  73. @Override
  74. public void onMapReady(GoogleMap googleMap) {
  75. mMap= googleMap;
  76.  
  77. // Add a marker in Sydney and move the camera
  78.  
  79. }
  80.  
  81. @Override
  82. public void onConnected(Bundle bundle) {
  83. requestLocationUpdates();
  84. }
  85.  
  86. private void requestLocationUpdates() {
  87. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  88. // TODO: Consider calling
  89. // ActivityCompat#requestPermissions
  90. // here to request the missing permissions, and then overriding
  91. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  92. // int[] grantResults)
  93. // to handle the case where the user grants the permission. See the documentation
  94. // for ActivityCompat#requestPermissions for more details.
  95. return;
  96. }
  97. LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
  98. }
  99.  
  100. @Override
  101. public void onConnectionSuspended(int i) {
  102.  
  103. }
  104.  
  105. @Override
  106. public void onConnectionFailed(ConnectionResult connectionResult) {
  107.  
  108. }
  109.  
  110.  
  111. @Override
  112. public void onLocationChanged(Location location) {
  113. myLatitude = location.getLatitude();
  114. myLongitude = location.getLongitude();
  115. latitudeText.setText("Latitude : " + String.valueOf(myLatitude));
  116. longitudeText.setText("Longitude : " + String.valueOf(myLongitude));
  117.  
  118. String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
  119.  
  120. LatLng sydney = new LatLng(myLatitude,myLongitude);
  121. mMap.addMarker(new MarkerOptions().position(sydney).title("Position du :"+currentDateTimeString));
  122. mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  123. }
  124.  
  125.  
  126. @Override
  127. protected void onStart() {
  128. super.onStart();
  129. googleApiClient.connect();
  130. }
  131.  
  132. @Override
  133. protected void onResume() {
  134. super.onResume();
  135. if (googleApiClient.isConnected()) {
  136. requestLocationUpdates();
  137. }
  138. }
  139.  
  140. @Override
  141. protected void onPause() {
  142. super.onPause();
  143. LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
  144. }
  145.  
  146. @Override
  147. protected void onStop() {
  148. super.onStop();
  149. googleApiClient.disconnect();
  150. }
  151.  
  152.  
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement