Advertisement
olcayertas

Google Map Fragment

Apr 4th, 2015
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.42 KB | None | 0 0
  1. package com.yceo.namazvakti.fragments;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.location.Location;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.support.v4.app.Fragment;
  9. import android.util.Log;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.RadioButton;
  14. import android.widget.RadioGroup;
  15.  
  16. import com.google.android.gms.common.ConnectionResult;
  17. import com.google.android.gms.common.api.GoogleApiClient;
  18. import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
  19. import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
  20. import com.google.android.gms.common.api.PendingResult;
  21. import com.google.android.gms.common.api.ResultCallback;
  22. import com.google.android.gms.location.places.PlaceLikelihood;
  23. import com.google.android.gms.location.places.PlaceLikelihoodBuffer;
  24. import com.google.android.gms.location.places.Places;
  25. import com.google.android.gms.maps.CameraUpdateFactory;
  26. import com.google.android.gms.maps.GoogleMap;
  27. import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
  28. import com.google.android.gms.maps.OnMapReadyCallback;
  29. import com.google.android.gms.maps.SupportMapFragment;
  30. import com.google.android.gms.maps.UiSettings;
  31. import com.google.android.gms.maps.model.LatLng;
  32. import com.yceo.namazvakti.R;
  33.  
  34. import info.hoang8f.android.segmented.SegmentedGroup;
  35.  
  36.  
  37. public class FindMosqueFragment extends Fragment implements
  38.         OnMapReadyCallback,
  39.         RadioGroup.OnCheckedChangeListener {
  40.     private static final String LOG_TAG = "Namaz Vakti";
  41.     private OnFragmentInteractionListener mListener;
  42.     private SegmentedGroup segmentedGroup;
  43.     private RadioButton listButton;
  44.     private RadioButton mapButton;
  45.     private SupportMapFragment mapFragment;
  46.     private View view;
  47.     private GoogleMap googleMap;
  48.     private Location mLastLocation;
  49.     private Boolean hasLastLocation = false;
  50.  
  51.     private class MyGoogleMapOnMyLocationListener implements
  52.             OnMyLocationChangeListener,
  53.             ConnectionCallbacks,
  54.             OnConnectionFailedListener {
  55.         private GoogleApiClient mGoogleApiClient;
  56.  
  57.         @Override
  58.         public void onMyLocationChange(Location location) {
  59.             if (!hasLastLocation) {
  60.                 hasLastLocation = true;
  61.                 mLastLocation = location;
  62.                 Log.d(LOG_TAG, "Location: " + location.toString());
  63.  
  64.                 if (googleMap != null ) {
  65.                     googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
  66.                             new LatLng(location.getLatitude(), location.getLongitude()), 16.0f));
  67.  
  68.                     if (mGoogleApiClient == null) {
  69.                         mGoogleApiClient = new GoogleApiClient
  70.                                 .Builder(getActivity())
  71.                                 .addApi(Places.GEO_DATA_API)
  72.                                 .addApi(Places.PLACE_DETECTION_API)
  73.                                 .addConnectionCallbacks(this)
  74.                                 .addOnConnectionFailedListener(this)
  75.                                 .build();
  76.                         mGoogleApiClient.connect();
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.  
  82.         @Override
  83.         public void onConnected(Bundle bundle) {
  84.             Log.d(LOG_TAG, "Google Api Client connected.");
  85.  
  86.             if (mGoogleApiClient != null) {
  87.                 Log.d(LOG_TAG, "Getting nearby places...");
  88.  
  89.                 PendingResult<PlaceLikelihoodBuffer> result =
  90.                         Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
  91.  
  92.                 result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
  93.                     @Override
  94.                     public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
  95.                         Log.d(LOG_TAG, "Got results: " + likelyPlaces.getCount() + " place found.");
  96.  
  97.                         for (PlaceLikelihood placeLikelihood : likelyPlaces) {
  98.                             Log.i(LOG_TAG, String.format("Place '%s' has likelihood: %g",
  99.                                     placeLikelihood.getPlace().getName(),
  100.                                     placeLikelihood.getLikelihood()));
  101.                         }
  102.  
  103.                         likelyPlaces.release();
  104.                     }
  105.                 });
  106.             }
  107.         }
  108.  
  109.         @Override
  110.         public void onConnectionSuspended(int i) {
  111.  
  112.         }
  113.  
  114.         @Override
  115.         public void onConnectionFailed(ConnectionResult connectionResult) {
  116.  
  117.         }
  118.     }
  119.  
  120.     public enum SegmentID {
  121.         LIST(0),
  122.         MAP(1);
  123.  
  124.         int value;
  125.  
  126.         SegmentID(int value){
  127.             this.value = value;
  128.         }
  129.  
  130.         public int getValue(){
  131.             return value;
  132.         }
  133.  
  134.         public static SegmentID fromInteger(int x) {
  135.  
  136.             switch(x) {
  137.                 case 0:
  138.                     return LIST;
  139.                 case 1:
  140.                     return MAP;
  141.             }
  142.  
  143.             return null;
  144.         }
  145.     }
  146.  
  147.  
  148.     public FindMosqueFragment() {
  149.         // Required empty public constructor
  150.     }
  151.  
  152.  
  153.     public static FindMosqueFragment newInstance() {
  154.         return new FindMosqueFragment();
  155.     }
  156.  
  157.  
  158.     @Override
  159.     public void onCreate(Bundle savedInstanceState) {
  160.         super.onCreate(savedInstanceState);
  161.     }
  162.  
  163.  
  164.     @Override
  165.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  166.  
  167.         if (view == null) {
  168.             view = inflater.inflate(R.layout.fragment_find_mosque, container, false);
  169.         }
  170.  
  171.         if (mapFragment == null) {
  172.             mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
  173.             mapFragment.getMapAsync(this);
  174.         }
  175.  
  176.         if (segmentedGroup == null) {
  177.             segmentedGroup = (SegmentedGroup) view.findViewById(R.id.segmentedControl);
  178.             segmentedGroup.setTintColor(Color.WHITE, Color.parseColor("#aaa194"));
  179.             segmentedGroup.setOnCheckedChangeListener(this);
  180.         }
  181.  
  182.         if (listButton == null) {
  183.             listButton = (RadioButton) view.findViewById(R.id.listButton);
  184.             listButton.setEnabled(true);
  185.             listButton.setClickable(true);
  186.             listButton.setId(SegmentID.LIST.getValue());
  187.         }
  188.  
  189.         if (mapButton == null) {
  190.             mapButton = (RadioButton) view.findViewById(R.id.mapButton);
  191.             mapButton.setId(SegmentID.MAP.getValue());
  192.             mapButton.setEnabled(true);
  193.             mapButton.setClickable(true);
  194.         }
  195.  
  196.         return view;
  197.     }
  198.  
  199.     public void onButtonPressed(Uri uri) {
  200.  
  201.         if (mListener != null) {
  202.             mListener.onFragmentInteraction(uri);
  203.         }
  204.     }
  205.  
  206.     @Override
  207.     public void onAttach(Activity activity) {
  208.         super.onAttach(activity);
  209.  
  210.         if (activity instanceof OnFragmentInteractionListener)
  211.             mListener = (OnFragmentInteractionListener) activity;
  212.     }
  213.  
  214.     @Override
  215.     public void onDetach() {
  216.         mListener = null;
  217.         super.onDetach();
  218.     }
  219.  
  220.     @Override
  221.     public void onMapReady(GoogleMap googleMap) {
  222.         UiSettings uiSettings = googleMap.getUiSettings();
  223.         uiSettings.setMyLocationButtonEnabled(true);
  224.         uiSettings.setCompassEnabled(true);
  225.         uiSettings.setMapToolbarEnabled(true);
  226.         uiSettings.setMyLocationButtonEnabled(true);
  227.         this.googleMap = googleMap;
  228.         googleMap.setIndoorEnabled(false);
  229.         googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  230.         googleMap.setMyLocationEnabled(true);
  231.         googleMap.setOnMyLocationChangeListener(new MyGoogleMapOnMyLocationListener());
  232.     }
  233.  
  234.     @Override
  235.     public void onCheckedChanged(RadioGroup group, int checkedId) {
  236.         Log.d(LOG_TAG, "Checked item id: " + checkedId);
  237.         final SegmentID si = SegmentID.fromInteger(checkedId);
  238.  
  239.         switch (si) {
  240.             case LIST:
  241.                 mapButton.setChecked(false);
  242.                 listButton.setChecked(true);
  243.                 break;
  244.             case MAP:
  245.                 mapButton.setChecked(true);
  246.                 listButton.setChecked(false);
  247.                 break;
  248.         }
  249.     }
  250.  
  251.     public interface OnFragmentInteractionListener {
  252.         public void onFragmentInteraction(Uri uri);
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement