Guest User

Untitled

a guest
Jul 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. private GoogleMap mMap;
  2. GoogleApiClient mGoogleApiClient;
  3. Marker mLocationMarker;
  4. Location mLastLocation;
  5. LocationRequest mLocationRequest;
  6. Button btnstop;
  7. LocationListener locationListener;
  8. LocationManager locationManager;
  9. private Roomdbse roomDatabase;
  10. private LiveData<List<EntityRoom>> mAllLocations;
  11. private EntityRoom loc;
  12.  
  13. @Override
  14.  
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_maps);
  18. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  19. checkLocationPermission();
  20. }
  21. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  22. .findFragmentById(R.id.map);
  23. mapFragment.getMapAsync(this);
  24. final Button button = (Button) findViewById(R.id.btnStop);
  25. getLocation();
  26.  
  27.  
  28.  
  29. }
  30.  
  31. public static final int MY_PERMISSIONS_REQUEST_LOCATION = 1;
  32.  
  33. @RequiresApi(api = Build.VERSION_CODES.M)
  34. public boolean checkLocationPermission() {
  35. if (ContextCompat.checkSelfPermission(this,
  36. android.Manifest.permission.ACCESS_COARSE_LOCATION)
  37. // If your app does have access to COARSE_LOCATION, then this method will return
  38. // PackageManager.PERMISSION_GRANTED//
  39. != PackageManager.PERMISSION_GRANTED) {
  40. if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  41. android.Manifest.permission.ACCESS_COARSE_LOCATION)) {
  42. // If your app doesn’t have this permission, then you’ll need to request it by calling
  43. // the ActivityCompat.requestPermissions method//
  44. requestPermissions(new String[]{
  45. android.Manifest.permission.ACCESS_COARSE_LOCATION
  46. },
  47. MY_PERMISSIONS_REQUEST_LOCATION);
  48. } else {
  49. // Request the permission by launching Android’s standard permissions dialog.
  50. // If you want to provide any additional information, such as why your app requires this
  51. // particular permission, then you’ll need to add this information before calling
  52. // requestPermission //
  53. requestPermissions(new String[]{
  54. android.Manifest.permission.ACCESS_COARSE_LOCATION
  55. },
  56. MY_PERMISSIONS_REQUEST_LOCATION);
  57. }
  58. return false;
  59. } else {
  60. return true;
  61. }
  62.  
  63.  
  64. }
  65.  
  66.  
  67.  
  68. @Override
  69. protected void onResume() {
  70. super.onResume();
  71.  
  72. }
  73.  
  74. @Override
  75. protected void onPause() {
  76. super.onPause();
  77. }
  78.  
  79. @Override
  80. public void onMapReady(GoogleMap googleMap) {
  81. mMap = googleMap;
  82. // Specify what kind of map you want to display. In this example I’m sticking with the
  83. // classic, “Normal” map
  84. mMap.setOnMarkerClickListener(this);
  85. mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  86.  
  87. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  88. if (ContextCompat.checkSelfPermission(this,
  89. android.Manifest.permission.ACCESS_COARSE_LOCATION)
  90. == PackageManager.PERMISSION_GRANTED) {
  91. buildGoogleApiClient();
  92. // Although the user’s location will update automatically on a regular basis, you can also
  93. // give your users a way of triggering a location update manually. Here, we’re adding a
  94. // ‘My Location’ button to the upper-right corner of our app; when the user taps this button,
  95. // the camera will update and center on the user’s current location//
  96.  
  97. mMap.setMyLocationEnabled(true);
  98. }
  99. } else {
  100. buildGoogleApiClient();
  101. mMap.setMyLocationEnabled(true);
  102. }
  103. }
  104.  
  105. protected synchronized void buildGoogleApiClient() {
  106. // Use the GoogleApiClient.Builder class to create an instance of the
  107. // Google Play Services API client//
  108. mGoogleApiClient = new GoogleApiClient.Builder(this)
  109. .addConnectionCallbacks(this)
  110. .addApi(LocationServices.API)
  111. .build();
  112.  
  113. // Connect to Google Play Services, by calling the connect() method//
  114. mGoogleApiClient.connect();
  115. }
  116.  
  117. @Override
  118. // If the connect request is completed successfully, the onConnected(Bundle) method
  119. // will be invoked and any queued items will be executed//
  120. public void onConnected(Bundle bundle) {
  121. mLocationRequest = new LocationRequest();
  122. mLocationRequest.setInterval(1000);
  123. mLocationRequest.setFastestInterval(5000);
  124.  
  125. if (ContextCompat.checkSelfPermission(this,
  126. android.Manifest.permission.ACCESS_COARSE_LOCATION)
  127. == PackageManager.PERMISSION_GRANTED) {
  128. // Retrieve the user’s last known location//
  129. LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
  130. mLocationRequest, this);
  131. }
  132. }
  133.  
  134. @Override
  135. public void onConnectionSuspended(int i) {
  136. }
  137.  
  138. // Displaying multiple ‘current location’ markers is only going to confuse your users!
  139. // To make sure there’s only ever one marker onscreen at a time, I’m using
  140. // mLocationMarker.remove to clear all markers whenever the user’s location changes.
  141.  
  142. @Override
  143. public void onLocationChanged(Location location) {
  144. mLastLocation = location;
  145. if (mLocationMarker != null) {
  146. mLocationMarker.remove();
  147. }
  148. if (mGoogleApiClient != null) {
  149. LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
  150. }
  151.  
  152.  
  153. }
  154.  
  155.  
  156. // Once the user has granted or denied your permission request, the Activity’s
  157. // onRequestPermissionsResult method will be called, and the system will pass
  158. // the results of the ‘grant permission’ dialog, as an int//
  159.  
  160. @Override
  161. public void onRequestPermissionsResult(int requestCode,
  162. String permissions[], int[] grantResults) {
  163. switch (requestCode) {
  164. case MY_PERMISSIONS_REQUEST_LOCATION: {
  165.  
  166. // If the request is cancelled, the result array will be empty (0)//
  167. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  168.  
  169. // If the user has granted your permission request, then your app can now perform all its
  170. // location-related tasks, including displaying the user’s location on the map//
  171. if (ContextCompat.checkSelfPermission(this,
  172. android.Manifest.permission.ACCESS_COARSE_LOCATION)
  173. == PackageManager.PERMISSION_GRANTED) {
  174. if (mGoogleApiClient == null) {
  175. buildGoogleApiClient();
  176. }
  177. mMap.setMyLocationEnabled(true);
  178. }
  179. } else {
  180. // If the user has denied your permission request, then at this point you may want to
  181. // disable any functionality that depends on this permission//
  182. }
  183. return;
  184. }
  185. }
  186. }
  187.  
  188. @Override
  189. public void onClick(View v) {
  190. btnstop.setOnClickListener((View.OnClickListener) this);
  191. if (v == btnstop) {
  192. locationManager.removeUpdates((android.location.LocationListener) locationListener);
  193. }
  194. }
  195.  
  196.  
  197. @Override
  198. public boolean onMarkerClick (Marker marker){
  199.  
  200.  
  201. MapsActivity map = new MapsActivity();
  202. map.getLocation();
  203. roomDatabase.getRoomdao().update(loc);
  204. return false;
  205.  
  206.  
  207.  
  208. }
  209.  
  210. public void getLocation(){
  211.  
  212. roomDatabase = Roomdbse.getInstance(MapsActivity.this);
  213. new RetrieveTask(this).execute();
  214. }
  215.  
  216.  
  217.  
  218. private static class RetrieveTask extends AsyncTask<Void,Void,List<EntityRoom>> {
  219.  
  220. private WeakReference<MapsActivity> activityReference;
  221. private EntityRoom loc;
  222.  
  223. // only retain a weak reference to the activity
  224. RetrieveTask(MapsActivity context) {
  225. activityReference = new WeakReference<>(context);
  226. }
  227.  
  228. @Override
  229. protected List<EntityRoom> doInBackground(Void... voids) {
  230.  
  231. activityReference.get().roomDatabase.getRoomdao().insert(loc);
  232. return (List<EntityRoom>) loc;
  233. }
  234. }
  235. }
  236.  
  237. private static class RetrieveTask extends AsyncTask<Void,Void,List<EntityRoom>> {
Add Comment
Please, Sign In to add comment