Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.trackit.app.track_it_v002;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.FragmentActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
- import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
- import com.google.android.gms.maps.StreetViewPanorama;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.MarkerOptions;
- import com.trackit.app.utilities.MapHandler;
- /**
- * 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
- * layout, extract this fragment from the later and use the MapFragment.getMapAsync() method to set the callback on the fragment.
- * 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.
- */
- public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
- OnStreetViewPanoramaReadyCallback,
- ConnectionCallbacks,
- OnConnectionFailedListener {
- private static final String TAG = MapsActivity.class.getSimpleName();
- public static final String STARTING_POSITION = "Rio de Janeiro"; //Application's default position
- public static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 1; //Fine location permission
- private int buttonPressed;
- private GoogleMap mMap; //Application's map
- private StreetViewPanorama mStreetView; //Application's street view
- private GoogleApiClient mGoogleApiClient; //Application's Google API client
- private MapHandler mHandler = new MapHandler(); //Application's map handler
- public static MapsActivity mActivity; //Application's map activity
- /**
- *
- * @param savedInstanceState
- *
- * This method is called when the Activity is first created.
- */
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- Log.e(TAG, "onCreate() started");
- super.onCreate(savedInstanceState);
- //Set the Activity's layout as the content view
- setContentView(R.layout.activity_maps);
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- //Set (a instance of) the callback on the fragment
- mapFragment.getMapAsync(this);
- if (mGoogleApiClient == null) {
- buildGoogleAPIClient();
- }
- //Store the Activity's current state
- mActivity = this;
- Log.e(TAG, "onCreate() terminated");
- }
- private synchronized void buildGoogleAPIClient() {
- //Configuring the Google API client before connect
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- }
- /**
- * This method is called when the Activity becomes visible to the user.
- */
- @Override
- protected void onStart() {
- Log.e(TAG, "onStart() started");
- super.onStart();
- mGoogleApiClient.connect();
- Log.e(TAG, "onStart() terminated");
- }
- /**
- * This method is called when the Activity is no longer visible to the user.
- */
- @Override
- protected void onStop() {
- Log.e(TAG, "onStop() started");
- if(mGoogleApiClient.isConnected())
- mGoogleApiClient.disconnect();
- super.onStop();
- Log.e(TAG, "onStop() terminated");
- }
- /**
- *
- * @param state
- *
- * This method called before the device orientation's changing.
- */
- protected void onSavedInstanceState(Bundle state) {
- // TODO Perguntar no StackOverflow a por que motivo continua dando erro ao rotacionar o dispositivo após usar onSavedInstanceState() e onRestoreInstaceState()
- Log.e(TAG, "onSavedInstanceState() started");
- //Save the Activity' state before the orientation's changing
- super.onSaveInstanceState(state);
- Log.e(TAG, "onSavedInstanceState() terminated");
- }
- /**
- *
- * @param savedInstanceState
- *
- * This method is called after the device orientation changing.
- */
- protected void onRestoInstanceState(Bundle savedInstanceState) {
- Log.e(TAG, "onRestoreInstanceState() started");
- //Restore the Activity's state after orientation's changing
- super.onRestoreInstanceState(savedInstanceState);
- Log.e(TAG, "onRestoreInstanceState() terminated");
- }
- /**
- * Manipulates the map once available.
- * This callback is triggered when the map is ready to be used.
- * This is where we can add markers or lines, add listeners or move the camera. In this case,
- * we just add a marker near Sydney, Australia.
- * If Google Play services is not installed on the device, the user will be prompted to install
- * it inside the SupportMapFragment. This method will only be triggered once the user has
- * installed Google Play services and returned to the app.
- */
- @Override
- public void onMapReady(GoogleMap googleMap) {
- Log.e(TAG, "onMapReady() started");
- LatLng startingPos = new LatLng(-22.907166, -43.176911); // Add a marker in the starting position and move the camera
- mMap = googleMap;
- //Configuring the map's settings
- mHandler.setMapType(mMap, GoogleMap.MAP_TYPE_NORMAL);
- mMap.setPadding(0, 0, 0, 100);
- mMap.getUiSettings().setZoomControlsEnabled(true);
- mMap.getUiSettings().setMapToolbarEnabled(false);
- mMap.getUiSettings().setAllGesturesEnabled(true);
- //Configuring the map's listeners
- mMap.setOnMapClickListener(mHandler);
- //Add the initial marker to the map and translate the camera to the starting position
- mMap.addMarker(new MarkerOptions().position(startingPos).title("Marker in " +
- STARTING_POSITION));
- mMap.moveCamera(CameraUpdateFactory.newLatLng(startingPos));
- //mActivity = this;
- Log.e(TAG, "onMapReady() terminated");
- }
- /**
- *
- * @param streetViewPanorama
- *
- * This callback is triggered when the Google Street View is ready to be used.
- */
- @Override
- public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
- Log.e(TAG, "onStreetViewPanoramaReady() started");
- mStreetView = streetViewPanorama;
- mStreetView.setPosition(new LatLng(-22.907166,-43.176911));
- Log.e(TAG, "onStreetViewPanoramaReady() terminated");
- }
- /**
- *
- * @param requestCode
- * @param permissions
- * @param grantResults
- *
- * This callback is triggered when the user answer the app's permission request; the user's response is passed as a parameter.
- */
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull
- int[] grantResults) {
- Log.e(TAG, "onRequestPermissionsResult() started");
- //Process the user permission's response
- switch (requestCode) {
- case MY_PERMISSIONS_REQUEST_FINE_LOCATION:
- mHandler.processLocationPermissionResult(grantResults);
- break;
- default:
- }
- Log.e(TAG, "onRequestPermissionsResult() terminated");
- }
- @Override
- public void onConnected(@Nullable Bundle bundle) {}
- @Override
- public void onConnectionSuspended(int i) {}
- @Override
- public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
- /**
- *
- * @param buttonPressed
- *
- * Method called when a UI button is pressed.
- */
- public void buttonPressed(View buttonPressed){
- this.buttonPressed = buttonPressed.getId();
- mActivity = this;
- switch(this.buttonPressed){
- case R.id.StreetView:
- mHandler.showStreetView(this);
- break;
- case R.id.Location:
- mHandler.displayUserLocation();
- break;
- default:
- }
- }
- public int getButtonPressed(){
- return buttonPressed;
- }
- /**
- *
- * @return
- *
- * Method used to return a Google Map's object.
- */
- public GoogleMap getMap(){
- return mMap;
- }
- /**
- *
- * @return
- *
- * Method used to return a Google Street View's object.
- */
- public StreetViewPanorama getStreetView(){
- return mStreetView;
- }
- /**
- *
- * @return
- *
- * Method used to return a Google API Client's object.
- */
- public GoogleApiClient getGoogleAPIClient(){
- return mGoogleApiClient;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement