Advertisement
Guest User

Shanx's a1

a guest
Sep 5th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.86 KB | None | 0 0
  1. package com.trackit.app.track_it_v002;
  2.  
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.FragmentActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9.  
  10. import com.google.android.gms.common.ConnectionResult;
  11. import com.google.android.gms.common.api.GoogleApiClient;
  12. import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
  13. import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
  14. import com.google.android.gms.location.LocationServices;
  15. import com.google.android.gms.maps.CameraUpdateFactory;
  16. import com.google.android.gms.maps.GoogleMap;
  17. import com.google.android.gms.maps.OnMapReadyCallback;
  18. import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
  19. import com.google.android.gms.maps.StreetViewPanorama;
  20. import com.google.android.gms.maps.SupportMapFragment;
  21. import com.google.android.gms.maps.model.LatLng;
  22. import com.google.android.gms.maps.model.MarkerOptions;
  23. import com.trackit.app.utilities.MapHandler;
  24.  
  25. /**
  26.  * The activity must implement the OnMapReadyCallback interface and set (a instance of) the callback on a MapFragment to be able to handle a map; to do so, is necessary to add a fragment to the Activity's
  27.  * layout, extract this fragment from the later and use the MapFragment.getMapAsync() method to set the callback on the fragment.
  28.  * The callback will be triggered from the main thread after the map be ready; if Google Plays Services isn't installed, the callback will not trigger until the user installs it.
  29.  */
  30. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
  31.                                                               OnStreetViewPanoramaReadyCallback,
  32.                                                               ConnectionCallbacks,
  33.                                                               OnConnectionFailedListener {
  34.  
  35.     private static final String TAG = MapsActivity.class.getSimpleName();
  36.     public static final String STARTING_POSITION = "Rio de Janeiro"; //Application's default position
  37.     public static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 1; //Fine location permission
  38.  
  39.     private int buttonPressed;
  40.     private GoogleMap mMap; //Application's map
  41.     private StreetViewPanorama mStreetView; //Application's street view
  42.     private GoogleApiClient mGoogleApiClient; //Application's Google API client
  43.     private MapHandler mHandler = new MapHandler(); //Application's map handler
  44.     public static MapsActivity mActivity; //Application's map activity
  45.  
  46.     /**
  47.      *
  48.      * @param savedInstanceState
  49.      *
  50.      * This method is called when the Activity is first created.
  51.      */
  52.     @Override
  53.     protected void onCreate(Bundle savedInstanceState) {
  54.  
  55.         Log.e(TAG, "onCreate() started");
  56.  
  57.         super.onCreate(savedInstanceState);
  58.  
  59.         //Set the Activity's layout as the content view
  60.         setContentView(R.layout.activity_maps);
  61.  
  62.         // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  63.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  64.                 .findFragmentById(R.id.map);
  65.  
  66.         //Set (a instance of) the callback on the fragment
  67.         mapFragment.getMapAsync(this);
  68.  
  69.         if (mGoogleApiClient == null) {
  70.  
  71.             buildGoogleAPIClient();
  72.  
  73.         }
  74.  
  75.         //Store the Activity's current state
  76.         mActivity = this;
  77.  
  78.         Log.e(TAG, "onCreate() terminated");
  79.  
  80.     }
  81.  
  82.     private synchronized void buildGoogleAPIClient() {
  83.  
  84.         //Configuring the Google API client before connect
  85.         mGoogleApiClient = new GoogleApiClient.Builder(this)
  86.                 .addConnectionCallbacks(this)
  87.                 .addOnConnectionFailedListener(this)
  88.                 .addApi(LocationServices.API)
  89.                 .build();
  90.  
  91.     }
  92.  
  93.     /**
  94.      * This method is called when the Activity becomes visible to the user.
  95.      */
  96.     @Override
  97.     protected void onStart() {
  98.  
  99.         Log.e(TAG, "onStart() started");
  100.  
  101.         super.onStart();
  102.         mGoogleApiClient.connect();
  103.  
  104.         Log.e(TAG, "onStart() terminated");
  105.  
  106.     }
  107.  
  108.     /**
  109.      * This method is called when the Activity is no longer visible to the user.
  110.      */
  111.     @Override
  112.     protected void onStop() {
  113.  
  114.         Log.e(TAG, "onStop() started");
  115.  
  116.         if(mGoogleApiClient.isConnected())
  117.             mGoogleApiClient.disconnect();
  118.  
  119.         super.onStop();
  120.  
  121.         Log.e(TAG, "onStop() terminated");
  122.  
  123.     }
  124.  
  125.     /**
  126.      *
  127.      * @param state
  128.      *
  129.      * This method called before the device orientation's changing.
  130.      */
  131.     protected void onSavedInstanceState(Bundle state) {
  132.  
  133.         // TODO Perguntar no StackOverflow a por que motivo continua dando erro ao rotacionar o dispositivo após usar onSavedInstanceState() e onRestoreInstaceState()
  134.  
  135.         Log.e(TAG, "onSavedInstanceState() started");
  136.  
  137.         //Save the Activity' state before the orientation's changing
  138.         super.onSaveInstanceState(state);
  139.  
  140.         Log.e(TAG, "onSavedInstanceState() terminated");
  141.  
  142.     }
  143.  
  144.     /**
  145.      *
  146.      * @param savedInstanceState
  147.      *
  148.      * This method is called after the device orientation changing.
  149.      */
  150.     protected void onRestoInstanceState(Bundle savedInstanceState) {
  151.  
  152.         Log.e(TAG, "onRestoreInstanceState() started");
  153.  
  154.         //Restore the Activity's state after orientation's changing
  155.         super.onRestoreInstanceState(savedInstanceState);
  156.  
  157.         Log.e(TAG, "onRestoreInstanceState() terminated");
  158.     }
  159.  
  160.     /**
  161.      * Manipulates the map once available.
  162.      * This callback is triggered when the map is ready to be used.
  163.      * This is where we can add markers or lines, add listeners or move the camera. In this case,
  164.      * we just add a marker near Sydney, Australia.
  165.      * If Google Play services is not installed on the device, the user will be prompted to install
  166.      * it inside the SupportMapFragment. This method will only be triggered once the user has
  167.      * installed Google Play services and returned to the app.
  168.      */
  169.     @Override
  170.     public void onMapReady(GoogleMap googleMap) {
  171.  
  172.         Log.e(TAG, "onMapReady() started");
  173.  
  174.         LatLng startingPos = new LatLng(-22.907166, -43.176911); // Add a marker in the starting position and move the camera
  175.  
  176.         mMap = googleMap;
  177.  
  178.         //Configuring the map's settings
  179.         mHandler.setMapType(mMap, GoogleMap.MAP_TYPE_NORMAL);
  180.         mMap.setPadding(0, 0, 0, 100);
  181.         mMap.getUiSettings().setZoomControlsEnabled(true);
  182.         mMap.getUiSettings().setMapToolbarEnabled(false);
  183.         mMap.getUiSettings().setAllGesturesEnabled(true);
  184.  
  185.         //Configuring the map's listeners
  186.         mMap.setOnMapClickListener(mHandler);
  187.  
  188.         //Add the initial marker to the map and translate the camera to the starting position
  189.         mMap.addMarker(new MarkerOptions().position(startingPos).title("Marker in " +
  190.                 STARTING_POSITION));
  191.         mMap.moveCamera(CameraUpdateFactory.newLatLng(startingPos));
  192.  
  193.         //mActivity = this;
  194.  
  195.         Log.e(TAG, "onMapReady() terminated");
  196.  
  197.     }
  198.  
  199.     /**
  200.      *
  201.      * @param streetViewPanorama
  202.      *
  203.      * This callback is triggered when the Google Street View is ready to be used.
  204.      */
  205.     @Override
  206.     public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
  207.  
  208.         Log.e(TAG, "onStreetViewPanoramaReady() started");
  209.  
  210.         mStreetView = streetViewPanorama;
  211.         mStreetView.setPosition(new LatLng(-22.907166,-43.176911));
  212.  
  213.         Log.e(TAG, "onStreetViewPanoramaReady() terminated");
  214.  
  215.     }
  216.  
  217.     /**
  218.      *
  219.      * @param requestCode
  220.      * @param permissions
  221.      * @param grantResults
  222.      *
  223.      * This callback is triggered when the user answer the app's permission request; the user's response is passed as a parameter.
  224.      */
  225.     @Override
  226.     public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull
  227.         int[] grantResults) {
  228.  
  229.         Log.e(TAG, "onRequestPermissionsResult() started");
  230.  
  231.         //Process the user permission's response
  232.         switch (requestCode) {
  233.             case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
  234.                 mHandler.processLocationPermissionResult(grantResults);
  235.                 break;
  236.             default:
  237.         }
  238.  
  239.         Log.e(TAG, "onRequestPermissionsResult() terminated");
  240.  
  241.     }
  242.  
  243.     @Override
  244.     public void onConnected(@Nullable Bundle bundle) {}
  245.  
  246.     @Override
  247.     public void onConnectionSuspended(int i) {}
  248.  
  249.     @Override
  250.     public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
  251.  
  252.     /**
  253.      *
  254.      * @param buttonPressed
  255.      *
  256.      * Method called when a UI button is pressed.
  257.      */
  258.     public void buttonPressed(View buttonPressed){
  259.  
  260.         this.buttonPressed = buttonPressed.getId();
  261.         mActivity = this;
  262.  
  263.         switch(this.buttonPressed){
  264.             case R.id.StreetView:
  265.                 mHandler.showStreetView(this);
  266.                 break;
  267.             case R.id.Location:
  268.                 mHandler.displayUserLocation();
  269.                 break;
  270.             default:
  271.         }
  272.  
  273.     }
  274.  
  275.     public int getButtonPressed(){
  276.  
  277.         return buttonPressed;
  278.  
  279.     }
  280.  
  281.     /**
  282.      *
  283.      * @return
  284.      *
  285.      * Method used to return a Google Map's object.
  286.      */
  287.     public GoogleMap getMap(){
  288.  
  289.         return mMap;
  290.  
  291.     }
  292.  
  293.     /**
  294.      *
  295.      * @return
  296.      *
  297.      * Method used to return a Google Street View's object.
  298.      */
  299.     public StreetViewPanorama getStreetView(){
  300.  
  301.         return mStreetView;
  302.  
  303.     }
  304.  
  305.     /**
  306.      *
  307.      * @return
  308.      *
  309.      * Method used to return a Google API Client's object.
  310.      */
  311.     public GoogleApiClient getGoogleAPIClient(){
  312.  
  313.         return mGoogleApiClient;
  314.  
  315.     }
  316.  
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement