Advertisement
AnAriyan

Map Activity Toura

Mar 14th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.16 KB | None | 0 0
  1. package com.androidtutorialpoint.googlemapsapp;
  2.  
  3. import android.Manifest;
  4. import android.content.pm.PackageManager;
  5. import android.location.Location;
  6. import android.os.Build;
  7. import android.support.v4.app.ActivityCompat;
  8. import android.support.v4.app.FragmentActivity;
  9. import android.os.Bundle;
  10. import android.support.v4.content.ContextCompat;
  11. import android.widget.Toast;
  12.  
  13. import com.google.android.gms.common.ConnectionResult;
  14. import com.google.android.gms.common.api.GoogleApiClient;
  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.BitmapDescriptorFactory;
  23. import com.google.android.gms.maps.model.LatLng;
  24. import com.google.android.gms.maps.model.Marker;
  25. import com.google.android.gms.maps.model.MarkerOptions;
  26.  
  27. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
  28.         GoogleApiClient.ConnectionCallbacks,
  29.         GoogleApiClient.OnConnectionFailedListener,
  30.         LocationListener {
  31.  
  32.     private GoogleMap mMap;
  33.     GoogleApiClient mGoogleApiClient;
  34.     Location mLastLocation;
  35.     Marker mCurrLocationMarker;
  36.     LocationRequest mLocationRequest;
  37.  
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_maps);
  42.  
  43.         if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  44.             checkLocationPermission();
  45.         }
  46.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  47.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  48.                 .findFragmentById(R.id.map);
  49.         mapFragment.getMapAsync(this);
  50.     }
  51.  
  52.  
  53.     /**
  54.      * Manipulates the map once available.
  55.      * This callback is triggered when the map is ready to be used.
  56.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  57.      * we just add a marker near Sydney, Australia.
  58.      * If Google Play services is not installed on the device, the user will be prompted to install
  59.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  60.      * installed Google Play services and returned to the app.
  61.      */
  62.     @Override
  63.     public void onMapReady(GoogleMap googleMap) {
  64.         mMap = googleMap;
  65.         mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  66.  
  67.         //Initialize Google Play Services
  68.         if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  69.             if (ContextCompat.checkSelfPermission(this,
  70.                     Manifest.permission.ACCESS_FINE_LOCATION)
  71.                     == PackageManager.PERMISSION_GRANTED) {
  72.                 buildGoogleApiClient();
  73.                 mMap.setMyLocationEnabled(true);
  74.             }
  75.         }
  76.         else {
  77.             buildGoogleApiClient();
  78.             mMap.setMyLocationEnabled(true);
  79.         }
  80.     }
  81.  
  82.     protected synchronized void buildGoogleApiClient() {
  83.         mGoogleApiClient = new GoogleApiClient.Builder(this)
  84.                 .addConnectionCallbacks(this)
  85.                 .addOnConnectionFailedListener(this)
  86.                 .addApi(LocationServices.API)
  87.                 .build();
  88.         mGoogleApiClient.connect();
  89.     }
  90.  
  91.     @Override
  92.     public void onConnected(Bundle bundle) {
  93.  
  94.         mLocationRequest = new LocationRequest();
  95.         mLocationRequest.setInterval(1000);
  96.         mLocationRequest.setFastestInterval(1000);
  97.         mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
  98.         if (ContextCompat.checkSelfPermission(this,
  99.                 Manifest.permission.ACCESS_FINE_LOCATION)
  100.                 == PackageManager.PERMISSION_GRANTED) {
  101.             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
  102.         }
  103.  
  104.     }
  105.  
  106.     @Override
  107.     public void onConnectionSuspended(int i) {
  108.  
  109.     }
  110.  
  111.     @Override
  112.     public void onLocationChanged(Location location) {
  113.  
  114.             mLastLocation = location;
  115.             if (mCurrLocationMarker != null) {
  116.                 mCurrLocationMarker.remove();
  117.             }
  118.  
  119.             //Place current location marker
  120.             LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
  121.             MarkerOptions markerOptions = new MarkerOptions();
  122.             markerOptions.position(latLng);
  123.             markerOptions.title("Current Position");
  124.             markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
  125.             mCurrLocationMarker = mMap.addMarker(markerOptions);
  126.  
  127.             //move map camera
  128.             mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  129.             mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
  130.  
  131.             //stop location updates
  132.             if (mGoogleApiClient != null) {
  133.                 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
  134.             }
  135.  
  136.     }
  137.  
  138.     @Override
  139.     public void onConnectionFailed(ConnectionResult connectionResult) {
  140.  
  141.     }
  142.  
  143.     public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
  144.     public boolean checkLocationPermission(){
  145.         if (ContextCompat.checkSelfPermission(this,
  146.                 Manifest.permission.ACCESS_FINE_LOCATION)
  147.                 != PackageManager.PERMISSION_GRANTED) {
  148.  
  149.             // Asking user if explanation is needed
  150.             if (ActivityCompat.shouldShowRequestPermissionRationale(this,
  151.                     Manifest.permission.ACCESS_FINE_LOCATION)) {
  152.  
  153.                 // Show an explanation to the user *asynchronously* -- don't block
  154.                 // this thread waiting for the user's response! After the user
  155.                 // sees the explanation, try again to request the permission.
  156.  
  157.                 //Prompt the user once explanation has been shown
  158.                 ActivityCompat.requestPermissions(this,
  159.                         new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  160.                         MY_PERMISSIONS_REQUEST_LOCATION);
  161.  
  162.  
  163.             } else {
  164.                 // No explanation needed, we can request the permission.
  165.                 ActivityCompat.requestPermissions(this,
  166.                         new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  167.                         MY_PERMISSIONS_REQUEST_LOCATION);
  168.             }
  169.             return false;
  170.         } else {
  171.             return true;
  172.         }
  173.     }
  174.  
  175.     @Override
  176.     public void onRequestPermissionsResult(int requestCode,
  177.                                            String permissions[], int[] grantResults) {
  178.         switch (requestCode) {
  179.             case MY_PERMISSIONS_REQUEST_LOCATION: {
  180.                 // If request is cancelled, the result arrays are empty.
  181.                 if (grantResults.length > 0
  182.                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  183.  
  184.                     // permission was granted. Do the
  185.                     // contacts-related task you need to do.
  186.                     if (ContextCompat.checkSelfPermission(this,
  187.                             Manifest.permission.ACCESS_FINE_LOCATION)
  188.                             == PackageManager.PERMISSION_GRANTED) {
  189.  
  190.                         if (mGoogleApiClient == null) {
  191.                             buildGoogleApiClient();
  192.                         }
  193.                         mMap.setMyLocationEnabled(true);
  194.                     }
  195.  
  196.                 } else {
  197.  
  198.                     // Permission denied, Disable the functionality that depends on this permission.
  199.                     Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
  200.                 }
  201.                 return;
  202.             }
  203.  
  204.             // other 'case' lines to check for other permissions this app might request.
  205.             // You can add here other case statements according to your requirement.
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement