Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private GoogleMap mMap;
- GoogleApiClient mGoogleApiClient;
- Marker mLocationMarker;
- Location mLastLocation;
- LocationRequest mLocationRequest;
- Button btnstop;
- LocationListener locationListener;
- LocationManager locationManager;
- private Roomdbse roomDatabase;
- private LiveData<List<EntityRoom>> mAllLocations;
- private EntityRoom loc;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- checkLocationPermission();
- }
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- final Button button = (Button) findViewById(R.id.btnStop);
- getLocation();
- }
- public static final int MY_PERMISSIONS_REQUEST_LOCATION = 1;
- @RequiresApi(api = Build.VERSION_CODES.M)
- public boolean checkLocationPermission() {
- if (ContextCompat.checkSelfPermission(this,
- android.Manifest.permission.ACCESS_COARSE_LOCATION)
- // If your app does have access to COARSE_LOCATION, then this method will return
- // PackageManager.PERMISSION_GRANTED//
- != PackageManager.PERMISSION_GRANTED) {
- if (ActivityCompat.shouldShowRequestPermissionRationale(this,
- android.Manifest.permission.ACCESS_COARSE_LOCATION)) {
- // If your app doesn’t have this permission, then you’ll need to request it by calling
- // the ActivityCompat.requestPermissions method//
- requestPermissions(new String[]{
- android.Manifest.permission.ACCESS_COARSE_LOCATION
- },
- MY_PERMISSIONS_REQUEST_LOCATION);
- } else {
- // Request the permission by launching Android’s standard permissions dialog.
- // If you want to provide any additional information, such as why your app requires this
- // particular permission, then you’ll need to add this information before calling
- // requestPermission //
- requestPermissions(new String[]{
- android.Manifest.permission.ACCESS_COARSE_LOCATION
- },
- MY_PERMISSIONS_REQUEST_LOCATION);
- }
- return false;
- } else {
- return true;
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- }
- @Override
- protected void onPause() {
- super.onPause();
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- // Specify what kind of map you want to display. In this example I’m sticking with the
- // classic, “Normal” map
- mMap.setOnMarkerClickListener(this);
- mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (ContextCompat.checkSelfPermission(this,
- android.Manifest.permission.ACCESS_COARSE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- buildGoogleApiClient();
- // Although the user’s location will update automatically on a regular basis, you can also
- // give your users a way of triggering a location update manually. Here, we’re adding a
- // ‘My Location’ button to the upper-right corner of our app; when the user taps this button,
- // the camera will update and center on the user’s current location//
- mMap.setMyLocationEnabled(true);
- }
- } else {
- buildGoogleApiClient();
- mMap.setMyLocationEnabled(true);
- }
- }
- protected synchronized void buildGoogleApiClient() {
- // Use the GoogleApiClient.Builder class to create an instance of the
- // Google Play Services API client//
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addApi(LocationServices.API)
- .build();
- // Connect to Google Play Services, by calling the connect() method//
- mGoogleApiClient.connect();
- }
- @Override
- // If the connect request is completed successfully, the onConnected(Bundle) method
- // will be invoked and any queued items will be executed//
- public void onConnected(Bundle bundle) {
- mLocationRequest = new LocationRequest();
- mLocationRequest.setInterval(1000);
- mLocationRequest.setFastestInterval(5000);
- if (ContextCompat.checkSelfPermission(this,
- android.Manifest.permission.ACCESS_COARSE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- // Retrieve the user’s last known location//
- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
- mLocationRequest, this);
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- // Displaying multiple ‘current location’ markers is only going to confuse your users!
- // To make sure there’s only ever one marker onscreen at a time, I’m using
- // mLocationMarker.remove to clear all markers whenever the user’s location changes.
- @Override
- public void onLocationChanged(Location location) {
- mLastLocation = location;
- if (mLocationMarker != null) {
- mLocationMarker.remove();
- }
- if (mGoogleApiClient != null) {
- LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
- }
- }
- // Once the user has granted or denied your permission request, the Activity’s
- // onRequestPermissionsResult method will be called, and the system will pass
- // the results of the ‘grant permission’ dialog, as an int//
- @Override
- public void onRequestPermissionsResult(int requestCode,
- String permissions[], int[] grantResults) {
- switch (requestCode) {
- case MY_PERMISSIONS_REQUEST_LOCATION: {
- // If the request is cancelled, the result array will be empty (0)//
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // If the user has granted your permission request, then your app can now perform all its
- // location-related tasks, including displaying the user’s location on the map//
- if (ContextCompat.checkSelfPermission(this,
- android.Manifest.permission.ACCESS_COARSE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- if (mGoogleApiClient == null) {
- buildGoogleApiClient();
- }
- mMap.setMyLocationEnabled(true);
- }
- } else {
- // If the user has denied your permission request, then at this point you may want to
- // disable any functionality that depends on this permission//
- }
- return;
- }
- }
- }
- @Override
- public void onClick(View v) {
- btnstop.setOnClickListener((View.OnClickListener) this);
- if (v == btnstop) {
- locationManager.removeUpdates((android.location.LocationListener) locationListener);
- }
- }
- @Override
- public boolean onMarkerClick (Marker marker){
- MapsActivity map = new MapsActivity();
- map.getLocation();
- roomDatabase.getRoomdao().update(loc);
- return false;
- }
- public void getLocation(){
- roomDatabase = Roomdbse.getInstance(MapsActivity.this);
- new RetrieveTask(this).execute();
- }
- private static class RetrieveTask extends AsyncTask<Void,Void,List<EntityRoom>> {
- private WeakReference<MapsActivity> activityReference;
- private EntityRoom loc;
- // only retain a weak reference to the activity
- RetrieveTask(MapsActivity context) {
- activityReference = new WeakReference<>(context);
- }
- @Override
- protected List<EntityRoom> doInBackground(Void... voids) {
- activityReference.get().roomDatabase.getRoomdao().insert(loc);
- return (List<EntityRoom>) loc;
- }
- }
- }
- private static class RetrieveTask extends AsyncTask<Void,Void,List<EntityRoom>> {
Add Comment
Please, Sign In to add comment