Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.syarif.syarifmaps.view;
- import android.content.Intent;
- import android.location.Location;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- 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.GoogleMap.OnMapClickListener;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.model.BitmapDescriptor;
- import com.google.android.gms.maps.model.BitmapDescriptorFactory;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.Marker;
- import com.google.android.gms.maps.model.MarkerOptions;
- import com.syarif.syarifmaps.R;
- import com.syarif.syarifmaps.database.DatabaseHelper;
- import java.util.List;
- import butterknife.ButterKnife;
- import butterknife.InjectView;
- import butterknife.OnClick;
- public class MainActivity extends AppCompatActivity implements
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- GoogleMap.OnInfoWindowClickListener {
- private static final int CATEGORY_ALL = 0;
- private static final int CATEGORY_SPBU = 1;
- private static final int CATEGORY_TMPT_IBADAH = 2;
- private static final int CATEGORY_KANTOR_DINAS = 4;
- private static final int CATEGORY_FOTO_COPY = 6;
- private static final float DEFAULT_ZOOM = 12;
- @InjectView(R.id.panelLeft)
- LinearLayout panelLeft;
- @InjectView(R.id.panelRight)
- LinearLayout panelRight;
- @InjectView(R.id.containerContent)
- RelativeLayout containerContent;
- private GoogleMap mMap;
- private Location location;
- private GoogleApiClient googleApiClient;
- private List<com.syarif.syarifmaps.entity.Location> locationList;
- private boolean panelLeftShow = false;
- private boolean panelRigtShow = false;
- private int[] mapType = { GoogleMap.MAP_TYPE_NORMAL,
- GoogleMap.MAP_TYPE_HYBRID, GoogleMap.MAP_TYPE_SATELLITE,
- GoogleMap.MAP_TYPE_TERRAIN };
- private int currentMapType = 0;
- private int maxMapType = mapType.length - 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ButterKnife.inject(this);
- // ambil data lokasi dari databse
- // masukkan kedalam arraylist
- locationList = DatabaseHelper.getInstance(this).getAllLocation();
- googleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this) // connection callback
- .addOnConnectionFailedListener(this) // when connection failed
- .addApi(LocationServices.API) // called api
- .build();
- setUpMapIfNeeded();
- mMap.setOnMapClickListener(new OnMapClickListener() {
- @Override
- public void onMapClick(LatLng point) {
- // TODO Auto-generated method stub
- if (panelLeftShow) {
- panelLeft.setVisibility(View.GONE);
- panelLeftShow = false;
- } else if (panelRigtShow) {
- panelRight.setVisibility(View.GONE);
- panelRigtShow = false;
- }
- }
- });
- // containerContent.setOnTouchListener(this);
- }
- @OnClick(R.id.btnSPBU)
- public void onBtnSpbuClick() {
- showMarker(CATEGORY_SPBU);
- }
- @OnClick(R.id.btnAll)
- public void onBtnAllClick() {
- showMarker(CATEGORY_ALL);
- }
- @OnClick(R.id.btnTempatIbadah)
- public void onBtnTempatIbadahClic() {
- showMarker(CATEGORY_TMPT_IBADAH);
- }
- @OnClick(R.id.btnKantorDinas)
- public void onBtnKantorDinasClick() {
- showMarker(CATEGORY_KANTOR_DINAS);
- }
- @OnClick(R.id.btnPercetakan)
- public void onBtnPercetakanClick() {
- showMarker(CATEGORY_FOTO_COPY);
- }
- @OnClick(R.id.btnTentang)
- public void onBtnTentangClick() {
- startActivity(new Intent(this, TentangActivity.class));
- }
- @OnClick(R.id.btnSejarah)
- public void onBtnSejarahClick() {
- startActivity(new Intent(this, SejarahActivity.class));
- }
- @OnClick(R.id.btnMapChange)
- public void onBtnMapChangeClick() {
- if (currentMapType < maxMapType) {
- currentMapType++;
- } else {
- currentMapType = 0;
- }
- Log.d("debug", "current map type " + currentMapType);
- mMap.setMapType(mapType[currentMapType]);
- }
- @OnClick(R.id.btnLeft)
- public void onBtnLeftClick() {
- Log.d("debug", "button left clicked");
- if (!panelLeftShow && !panelRigtShow) {
- // show panel
- panelLeft.setVisibility(View.VISIBLE);
- panelLeftShow = true;
- } else {
- // hide panel
- panelLeft.setVisibility(View.GONE);
- panelLeftShow = false;
- }
- }
- @OnClick(R.id.btnRight)
- public void onBtnRightClick() {
- if (!panelRigtShow && !panelLeftShow) {
- // tidak nampak
- panelRight.setVisibility(View.VISIBLE);
- panelRigtShow = true;
- setContainerBackground(true);
- } else {
- // nampak
- panelRight.setVisibility(View.GONE);
- panelRigtShow = false;
- setContainerBackground(false);
- }
- }
- @OnClick(R.id.btnListLokasi)
- public void onBtnListLokasiClick() {
- startActivity(new Intent(this, ListLokasiActivity.class));
- }
- private void setContainerBackground(boolean set) {
- if (set) {
- // containerContent.setAlpha(50);
- } else {
- // containerContent.setAlpha(0);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- // noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- /**
- * setupMaf
- */
- private void setUpMapIfNeeded() {
- // Do a null check to confirm that we have not already instantiated the
- // map.
- if (mMap == null) {
- // Try to obtain the map from the SupportMapFragment.
- mMap = ((SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map)).getMap();
- // Check if we were successful in obtaining the map.
- if (mMap != null) {
- setUpMap();
- }
- }
- }
- private void setUpMap() {
- mMap.setOnInfoWindowClickListener(this);
- mMap.setMyLocationEnabled(true);
- showMarker(CATEGORY_ALL);
- }
- private void showMarker(int category) {
- // clear map
- mMap.clear();
- // jika yang ingin ditampilkan semua dat
- if (category == CATEGORY_ALL) {
- for (int i = 0; i < locationList.size(); i++) {
- // ---- pilih icon untuk marker START----
- BitmapDescriptor icon = null;
- if (locationList.get(i).getCategory() == CATEGORY_SPBU) {
- icon = BitmapDescriptorFactory
- .fromResource(R.drawable.ic_marker_spbu);
- } else if (locationList.get(i).getCategory() == CATEGORY_TMPT_IBADAH) {
- icon = BitmapDescriptorFactory
- .fromResource(R.drawable.ic_marker_tempatibadah);
- } else if (locationList.get(i).getCategory() == CATEGORY_FOTO_COPY) {
- icon = BitmapDescriptorFactory
- .fromResource(R.drawable.ic_marker_foto_copy);
- } else if (locationList.get(i).getCategory() == CATEGORY_KANTOR_DINAS) {
- icon = BitmapDescriptorFactory
- .fromResource(R.drawable.ic_marker_kantor_dinas);
- }
- // ---- pilih icon untuk marker END----
- // tambahkan marker ke map START
- mMap.addMarker(new MarkerOptions()
- .position(
- new LatLng(locationList.get(i).getLat(),
- locationList.get(i).getLng()))
- .title(locationList.get(i).getName()).icon(icon));
- // tambahkan marker ke map END
- }
- } else if (category == CATEGORY_SPBU) {
- // show marker SPBU
- for (int i = 0; i < locationList.size(); i++) {
- BitmapDescriptor icon = null;
- if (locationList.get(i).getCategory() == CATEGORY_SPBU) {
- // set marker untuk SPBU
- icon = BitmapDescriptorFactory
- .fromResource(R.drawable.ic_marker_spbu);
- // tambahkan marker ke map START
- mMap.addMarker(new MarkerOptions()
- .position(
- new LatLng(locationList.get(i).getLat(),
- locationList.get(i).getLng()))
- .title(locationList.get(i).getName()).icon(icon));
- // tambahkan marker ke map END
- }
- }
- } else if (category == CATEGORY_TMPT_IBADAH) {
- // show marker temapt ibadah
- for (int i = 0; i < locationList.size(); i++) {
- BitmapDescriptor icon = null;
- // cek a
- if (locationList.get(i).getCategory() == CATEGORY_TMPT_IBADAH) {
- // set icon untuk tempat ibadah
- icon = BitmapDescriptorFactory
- .fromResource(R.drawable.ic_marker_tempatibadah);
- // tambahkan marker ke map START
- mMap.addMarker(new MarkerOptions()
- .position(
- new LatLng(locationList.get(i).getLat(),
- locationList.get(i).getLng()))
- .title(locationList.get(i).getName()).icon(icon));
- // tambahkan marker ke map END
- }
- }
- } else if (category == CATEGORY_KANTOR_DINAS) {
- for (int i = 0; i < locationList.size(); i++) {
- BitmapDescriptor icon = null;
- // cek a
- if (locationList.get(i).getCategory() == CATEGORY_KANTOR_DINAS) {
- // set icon untuk tempat ibadah
- icon = BitmapDescriptorFactory
- .fromResource(R.drawable.ic_marker_kantor_dinas);
- // tambahkan marker ke map START
- mMap.addMarker(new MarkerOptions()
- .position(
- new LatLng(locationList.get(i).getLat(),
- locationList.get(i).getLng()))
- .title(locationList.get(i).getName()).icon(icon));
- // tambahkan marker ke map END
- }
- }
- } else if (category == CATEGORY_FOTO_COPY) {
- for (int i = 0; i < locationList.size(); i++) {
- BitmapDescriptor icon = null;
- // cek a
- if (locationList.get(i).getCategory() == CATEGORY_FOTO_COPY) {
- // set icon untuk tempat ibadah
- icon = BitmapDescriptorFactory
- .fromResource(R.drawable.ic_marker_foto_copy);
- // tambahkan marker ke map START
- mMap.addMarker(new MarkerOptions()
- .position(
- new LatLng(locationList.get(i).getLat(),
- locationList.get(i).getLng()))
- .title(locationList.get(i).getName()).icon(icon));
- // tambahkan marker ke map END
- }
- }
- }
- }
- /**
- * method ini berjalan ketika info windows di klik oleh user
- *
- * @param marker
- */
- @Override
- public void onInfoWindowClick(Marker marker) {
- Bundle data = new Bundle();
- // get title marker
- data.putString("title", marker.getTitle());
- // pindah activity ke DetailLocation
- startActivity(new Intent(this, DetailLocationActivity.class)
- .putExtras(data));
- }
- @Override
- protected void onResume() {
- super.onResume();
- setUpMapIfNeeded();
- }
- @Override
- protected void onStart() {
- super.onStart();
- googleApiClient.connect();
- }
- @Override
- protected void onStop() {
- super.onStop();
- googleApiClient.disconnect();
- }
- @Override
- public void onConnected(Bundle bundle) {
- if (location == null) {
- // get last location device
- location = LocationServices.FusedLocationApi
- .getLastLocation(googleApiClient);
- if (location != null) {
- // animasikan camera maps ke lokasi user sekarang
- mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
- new LatLng(location.getLatitude(), location
- .getLongitude()), DEFAULT_ZOOM));
- }
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- }
- // @Override
- // public boolean onTouch(View arg0, MotionEvent event) {
- // // TODO Auto-generated method stub
- // switch (event.getAction()) {
- // case MotionEvent.ACTION_UP:
- // Log.d("debug", "gerakk...");
- // if (panelLeftShow) {
- // panelLeft.setVisibility(View.GONE);
- // panelLeftShow = false;
- // }
- // break;
- // }
- // return true;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement