Advertisement
Guest User

MainActivity.java

a guest
May 26th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.22 KB | None | 0 0
  1. package com.syarif.syarifmaps.view;
  2.  
  3. import android.content.Intent;
  4. import android.location.Location;
  5. import android.os.Bundle;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.util.Log;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.view.View.OnTouchListener;
  13. import android.widget.LinearLayout;
  14. import android.widget.RelativeLayout;
  15. import android.widget.Toast;
  16.  
  17. import com.google.android.gms.common.ConnectionResult;
  18. import com.google.android.gms.common.api.GoogleApiClient;
  19. import com.google.android.gms.location.LocationServices;
  20. import com.google.android.gms.maps.CameraUpdateFactory;
  21. import com.google.android.gms.maps.GoogleMap;
  22. import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
  23. import com.google.android.gms.maps.SupportMapFragment;
  24. import com.google.android.gms.maps.model.BitmapDescriptor;
  25. import com.google.android.gms.maps.model.BitmapDescriptorFactory;
  26. import com.google.android.gms.maps.model.LatLng;
  27. import com.google.android.gms.maps.model.Marker;
  28. import com.google.android.gms.maps.model.MarkerOptions;
  29. import com.syarif.syarifmaps.R;
  30. import com.syarif.syarifmaps.database.DatabaseHelper;
  31.  
  32. import java.util.List;
  33.  
  34. import butterknife.ButterKnife;
  35. import butterknife.InjectView;
  36. import butterknife.OnClick;
  37.  
  38. public class MainActivity extends AppCompatActivity implements
  39.         GoogleApiClient.ConnectionCallbacks,
  40.         GoogleApiClient.OnConnectionFailedListener,
  41.         GoogleMap.OnInfoWindowClickListener {
  42.  
  43.     private static final int CATEGORY_ALL = 0;
  44.     private static final int CATEGORY_SPBU = 1;
  45.     private static final int CATEGORY_TMPT_IBADAH = 2;
  46.     private static final int CATEGORY_KANTOR_DINAS = 4;
  47.     private static final int CATEGORY_FOTO_COPY = 6;
  48.     private static final float DEFAULT_ZOOM = 12;
  49.  
  50.     @InjectView(R.id.panelLeft)
  51.     LinearLayout panelLeft;
  52.     @InjectView(R.id.panelRight)
  53.     LinearLayout panelRight;
  54.     @InjectView(R.id.containerContent)
  55.     RelativeLayout containerContent;
  56.  
  57.     private GoogleMap mMap;
  58.     private Location location;
  59.     private GoogleApiClient googleApiClient;
  60.     private List<com.syarif.syarifmaps.entity.Location> locationList;
  61.  
  62.     private boolean panelLeftShow = false;
  63.     private boolean panelRigtShow = false;
  64.  
  65.     private int[] mapType = { GoogleMap.MAP_TYPE_NORMAL,
  66.             GoogleMap.MAP_TYPE_HYBRID, GoogleMap.MAP_TYPE_SATELLITE,
  67.             GoogleMap.MAP_TYPE_TERRAIN };
  68.  
  69.     private int currentMapType = 0;
  70.     private int maxMapType = mapType.length - 1;
  71.  
  72.     @Override
  73.     protected void onCreate(Bundle savedInstanceState) {
  74.         super.onCreate(savedInstanceState);
  75.         setContentView(R.layout.activity_main);
  76.         ButterKnife.inject(this);
  77.  
  78.         // ambil data lokasi dari databse
  79.         // masukkan kedalam arraylist
  80.         locationList = DatabaseHelper.getInstance(this).getAllLocation();
  81.  
  82.         googleApiClient = new GoogleApiClient.Builder(this)
  83.                 .addConnectionCallbacks(this) // connection callback
  84.                 .addOnConnectionFailedListener(this) // when connection failed
  85.                 .addApi(LocationServices.API) // called api
  86.                 .build();
  87.  
  88.         setUpMapIfNeeded();
  89.  
  90.         mMap.setOnMapClickListener(new OnMapClickListener() {
  91.  
  92.             @Override
  93.             public void onMapClick(LatLng point) {
  94.                 // TODO Auto-generated method stub
  95.                 if (panelLeftShow) {
  96.                     panelLeft.setVisibility(View.GONE);
  97.                     panelLeftShow = false;
  98.                 } else if (panelRigtShow) {
  99.                     panelRight.setVisibility(View.GONE);
  100.                     panelRigtShow = false;
  101.                 }
  102.             }
  103.         });
  104.  
  105.         // containerContent.setOnTouchListener(this);
  106.     }
  107.  
  108.     @OnClick(R.id.btnSPBU)
  109.     public void onBtnSpbuClick() {
  110.         showMarker(CATEGORY_SPBU);
  111.     }
  112.  
  113.     @OnClick(R.id.btnAll)
  114.     public void onBtnAllClick() {
  115.         showMarker(CATEGORY_ALL);
  116.     }
  117.  
  118.     @OnClick(R.id.btnTempatIbadah)
  119.     public void onBtnTempatIbadahClic() {
  120.         showMarker(CATEGORY_TMPT_IBADAH);
  121.     }
  122.  
  123.     @OnClick(R.id.btnKantorDinas)
  124.     public void onBtnKantorDinasClick() {
  125.         showMarker(CATEGORY_KANTOR_DINAS);
  126.     }
  127.  
  128.     @OnClick(R.id.btnPercetakan)
  129.     public void onBtnPercetakanClick() {
  130.         showMarker(CATEGORY_FOTO_COPY);
  131.     }
  132.  
  133.     @OnClick(R.id.btnTentang)
  134.     public void onBtnTentangClick() {
  135.         startActivity(new Intent(this, TentangActivity.class));
  136.     }
  137.  
  138.     @OnClick(R.id.btnSejarah)
  139.     public void onBtnSejarahClick() {
  140.         startActivity(new Intent(this, SejarahActivity.class));
  141.     }
  142.  
  143.     @OnClick(R.id.btnMapChange)
  144.     public void onBtnMapChangeClick() {
  145.         if (currentMapType < maxMapType) {
  146.             currentMapType++;
  147.         } else {
  148.             currentMapType = 0;
  149.         }
  150.         Log.d("debug", "current map type " + currentMapType);
  151.         mMap.setMapType(mapType[currentMapType]);
  152.     }
  153.  
  154.     @OnClick(R.id.btnLeft)
  155.     public void onBtnLeftClick() {
  156.         Log.d("debug", "button left clicked");
  157.         if (!panelLeftShow && !panelRigtShow) {
  158.             // show panel
  159.             panelLeft.setVisibility(View.VISIBLE);
  160.             panelLeftShow = true;
  161.  
  162.         } else {
  163.             // hide panel
  164.             panelLeft.setVisibility(View.GONE);
  165.             panelLeftShow = false;
  166.         }
  167.     }
  168.  
  169.     @OnClick(R.id.btnRight)
  170.     public void onBtnRightClick() {
  171.         if (!panelRigtShow && !panelLeftShow) {
  172.             // tidak nampak
  173.             panelRight.setVisibility(View.VISIBLE);
  174.             panelRigtShow = true;
  175.             setContainerBackground(true);
  176.         } else {
  177.             // nampak
  178.             panelRight.setVisibility(View.GONE);
  179.             panelRigtShow = false;
  180.             setContainerBackground(false);
  181.         }
  182.     }
  183.  
  184.     @OnClick(R.id.btnListLokasi)
  185.     public void onBtnListLokasiClick() {
  186.         startActivity(new Intent(this, ListLokasiActivity.class));
  187.     }
  188.  
  189.     private void setContainerBackground(boolean set) {
  190.         if (set) {
  191.             // containerContent.setAlpha(50);
  192.         } else {
  193.             // containerContent.setAlpha(0);
  194.         }
  195.     }
  196.  
  197.     @Override
  198.     public boolean onCreateOptionsMenu(Menu menu) {
  199.         // Inflate the menu; this adds items to the action bar if it is present.
  200.         getMenuInflater().inflate(R.menu.menu_main, menu);
  201.         return true;
  202.     }
  203.  
  204.     @Override
  205.     public boolean onOptionsItemSelected(MenuItem item) {
  206.         // Handle action bar item clicks here. The action bar will
  207.         // automatically handle clicks on the Home/Up button, so long
  208.         // as you specify a parent activity in AndroidManifest.xml.
  209.         int id = item.getItemId();
  210.  
  211.         // noinspection SimplifiableIfStatement
  212.         if (id == R.id.action_settings) {
  213.             return true;
  214.         }
  215.  
  216.         return super.onOptionsItemSelected(item);
  217.     }
  218.  
  219.     /**
  220.      * setupMaf
  221.      */
  222.     private void setUpMapIfNeeded() {
  223.         // Do a null check to confirm that we have not already instantiated the
  224.         // map.
  225.         if (mMap == null) {
  226.             // Try to obtain the map from the SupportMapFragment.
  227.             mMap = ((SupportMapFragment) getSupportFragmentManager()
  228.                     .findFragmentById(R.id.map)).getMap();
  229.             // Check if we were successful in obtaining the map.
  230.             if (mMap != null) {
  231.                 setUpMap();
  232.             }
  233.         }
  234.     }
  235.  
  236.     private void setUpMap() {
  237.         mMap.setOnInfoWindowClickListener(this);
  238.         mMap.setMyLocationEnabled(true);
  239.         showMarker(CATEGORY_ALL);
  240.     }
  241.  
  242.     private void showMarker(int category) {
  243.         // clear map
  244.         mMap.clear();
  245.  
  246.         // jika yang ingin ditampilkan semua dat
  247.         if (category == CATEGORY_ALL) {
  248.             for (int i = 0; i < locationList.size(); i++) {
  249.  
  250.                 // ---- pilih icon untuk marker START----
  251.                 BitmapDescriptor icon = null;
  252.                 if (locationList.get(i).getCategory() == CATEGORY_SPBU) {
  253.                     icon = BitmapDescriptorFactory
  254.                             .fromResource(R.drawable.ic_marker_spbu);
  255.                 } else if (locationList.get(i).getCategory() == CATEGORY_TMPT_IBADAH) {
  256.                     icon = BitmapDescriptorFactory
  257.                             .fromResource(R.drawable.ic_marker_tempatibadah);
  258.                 } else if (locationList.get(i).getCategory() == CATEGORY_FOTO_COPY) {
  259.                     icon = BitmapDescriptorFactory
  260.                             .fromResource(R.drawable.ic_marker_foto_copy);
  261.                 } else if (locationList.get(i).getCategory() == CATEGORY_KANTOR_DINAS) {
  262.                     icon = BitmapDescriptorFactory
  263.                             .fromResource(R.drawable.ic_marker_kantor_dinas);
  264.                 }
  265.                 // ---- pilih icon untuk marker END----
  266.  
  267.                 // tambahkan marker ke map START
  268.                 mMap.addMarker(new MarkerOptions()
  269.                         .position(
  270.                                 new LatLng(locationList.get(i).getLat(),
  271.                                         locationList.get(i).getLng()))
  272.                         .title(locationList.get(i).getName()).icon(icon));
  273.                 // tambahkan marker ke map END
  274.             }
  275.         } else if (category == CATEGORY_SPBU) {
  276.             // show marker SPBU
  277.             for (int i = 0; i < locationList.size(); i++) {
  278.                 BitmapDescriptor icon = null;
  279.                 if (locationList.get(i).getCategory() == CATEGORY_SPBU) {
  280.                     // set marker untuk SPBU
  281.                     icon = BitmapDescriptorFactory
  282.                             .fromResource(R.drawable.ic_marker_spbu);
  283.  
  284.                     // tambahkan marker ke map START
  285.                     mMap.addMarker(new MarkerOptions()
  286.                             .position(
  287.                                     new LatLng(locationList.get(i).getLat(),
  288.                                             locationList.get(i).getLng()))
  289.                             .title(locationList.get(i).getName()).icon(icon));
  290.                     // tambahkan marker ke map END
  291.                 }
  292.             }
  293.  
  294.         } else if (category == CATEGORY_TMPT_IBADAH) {
  295.             // show marker temapt ibadah
  296.             for (int i = 0; i < locationList.size(); i++) {
  297.                 BitmapDescriptor icon = null;
  298.                 // cek a
  299.                 if (locationList.get(i).getCategory() == CATEGORY_TMPT_IBADAH) {
  300.                     // set icon untuk tempat ibadah
  301.                     icon = BitmapDescriptorFactory
  302.                             .fromResource(R.drawable.ic_marker_tempatibadah);
  303.  
  304.                     // tambahkan marker ke map START
  305.                     mMap.addMarker(new MarkerOptions()
  306.                             .position(
  307.                                     new LatLng(locationList.get(i).getLat(),
  308.                                             locationList.get(i).getLng()))
  309.                             .title(locationList.get(i).getName()).icon(icon));
  310.                     // tambahkan marker ke map END
  311.                 }
  312.             }
  313.         } else if (category == CATEGORY_KANTOR_DINAS) {
  314.             for (int i = 0; i < locationList.size(); i++) {
  315.                 BitmapDescriptor icon = null;
  316.                 // cek a
  317.                 if (locationList.get(i).getCategory() == CATEGORY_KANTOR_DINAS) {
  318.                     // set icon untuk tempat ibadah
  319.                     icon = BitmapDescriptorFactory
  320.                             .fromResource(R.drawable.ic_marker_kantor_dinas);
  321.  
  322.                     // tambahkan marker ke map START
  323.                     mMap.addMarker(new MarkerOptions()
  324.                             .position(
  325.                                     new LatLng(locationList.get(i).getLat(),
  326.                                             locationList.get(i).getLng()))
  327.                             .title(locationList.get(i).getName()).icon(icon));
  328.                     // tambahkan marker ke map END
  329.                 }
  330.             }
  331.         } else if (category == CATEGORY_FOTO_COPY) {
  332.             for (int i = 0; i < locationList.size(); i++) {
  333.                 BitmapDescriptor icon = null;
  334.                 // cek a
  335.                 if (locationList.get(i).getCategory() == CATEGORY_FOTO_COPY) {
  336.                     // set icon untuk tempat ibadah
  337.                     icon = BitmapDescriptorFactory
  338.                             .fromResource(R.drawable.ic_marker_foto_copy);
  339.  
  340.                     // tambahkan marker ke map START
  341.                     mMap.addMarker(new MarkerOptions()
  342.                             .position(
  343.                                     new LatLng(locationList.get(i).getLat(),
  344.                                             locationList.get(i).getLng()))
  345.                             .title(locationList.get(i).getName()).icon(icon));
  346.                     // tambahkan marker ke map END
  347.                 }
  348.             }
  349.         }
  350.  
  351.     }
  352.  
  353.     /**
  354.      * method ini berjalan ketika info windows di klik oleh user
  355.      *
  356.      * @param marker
  357.      */
  358.     @Override
  359.     public void onInfoWindowClick(Marker marker) {
  360.         Bundle data = new Bundle();
  361.         // get title marker
  362.         data.putString("title", marker.getTitle());
  363.  
  364.         // pindah activity ke DetailLocation
  365.         startActivity(new Intent(this, DetailLocationActivity.class)
  366.                 .putExtras(data));
  367.     }
  368.  
  369.     @Override
  370.     protected void onResume() {
  371.         super.onResume();
  372.         setUpMapIfNeeded();
  373.     }
  374.  
  375.     @Override
  376.     protected void onStart() {
  377.         super.onStart();
  378.         googleApiClient.connect();
  379.     }
  380.  
  381.     @Override
  382.     protected void onStop() {
  383.         super.onStop();
  384.         googleApiClient.disconnect();
  385.     }
  386.  
  387.     @Override
  388.     public void onConnected(Bundle bundle) {
  389.         if (location == null) {
  390.             // get last location device
  391.             location = LocationServices.FusedLocationApi
  392.                     .getLastLocation(googleApiClient);
  393.  
  394.             if (location != null) {
  395.                 // animasikan camera maps ke lokasi user sekarang
  396.                 mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
  397.                         new LatLng(location.getLatitude(), location
  398.                                 .getLongitude()), DEFAULT_ZOOM));
  399.  
  400.             }
  401.         }
  402.     }
  403.  
  404.     @Override
  405.     public void onConnectionSuspended(int i) {
  406.  
  407.     }
  408.  
  409.     @Override
  410.     public void onConnectionFailed(ConnectionResult connectionResult) {
  411.  
  412.     }
  413.  
  414.     // @Override
  415.     // public boolean onTouch(View arg0, MotionEvent event) {
  416.     // // TODO Auto-generated method stub
  417.     // switch (event.getAction()) {
  418.     // case MotionEvent.ACTION_UP:
  419.     // Log.d("debug", "gerakk...");
  420.     // if (panelLeftShow) {
  421.     // panelLeft.setVisibility(View.GONE);
  422.     // panelLeftShow = false;
  423.     // }
  424.     // break;
  425.     // }
  426.     // return true;
  427.     // }
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement