Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.yceo.namazvakti.fragments;
- import android.app.Activity;
- import android.graphics.Color;
- import android.location.Location;
- import android.net.Uri;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- 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.common.api.PendingResult;
- import com.google.android.gms.common.api.ResultCallback;
- import com.google.android.gms.location.places.PlaceLikelihood;
- import com.google.android.gms.location.places.PlaceLikelihoodBuffer;
- import com.google.android.gms.location.places.Places;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
- import com.google.android.gms.maps.OnMapReadyCallback;
- import com.google.android.gms.maps.SupportMapFragment;
- import com.google.android.gms.maps.UiSettings;
- import com.google.android.gms.maps.model.LatLng;
- import com.yceo.namazvakti.R;
- import info.hoang8f.android.segmented.SegmentedGroup;
- public class FindMosqueFragment extends Fragment implements
- OnMapReadyCallback,
- RadioGroup.OnCheckedChangeListener {
- private static final String LOG_TAG = "Namaz Vakti";
- private OnFragmentInteractionListener mListener;
- private SegmentedGroup segmentedGroup;
- private RadioButton listButton;
- private RadioButton mapButton;
- private SupportMapFragment mapFragment;
- private View view;
- private GoogleMap googleMap;
- private Location mLastLocation;
- private Boolean hasLastLocation = false;
- private class MyGoogleMapOnMyLocationListener implements
- OnMyLocationChangeListener,
- ConnectionCallbacks,
- OnConnectionFailedListener {
- private GoogleApiClient mGoogleApiClient;
- @Override
- public void onMyLocationChange(Location location) {
- if (!hasLastLocation) {
- hasLastLocation = true;
- mLastLocation = location;
- Log.d(LOG_TAG, "Location: " + location.toString());
- if (googleMap != null ) {
- googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
- new LatLng(location.getLatitude(), location.getLongitude()), 16.0f));
- if (mGoogleApiClient == null) {
- mGoogleApiClient = new GoogleApiClient
- .Builder(getActivity())
- .addApi(Places.GEO_DATA_API)
- .addApi(Places.PLACE_DETECTION_API)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .build();
- mGoogleApiClient.connect();
- }
- }
- }
- }
- @Override
- public void onConnected(Bundle bundle) {
- Log.d(LOG_TAG, "Google Api Client connected.");
- if (mGoogleApiClient != null) {
- Log.d(LOG_TAG, "Getting nearby places...");
- PendingResult<PlaceLikelihoodBuffer> result =
- Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
- result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
- @Override
- public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
- Log.d(LOG_TAG, "Got results: " + likelyPlaces.getCount() + " place found.");
- for (PlaceLikelihood placeLikelihood : likelyPlaces) {
- Log.i(LOG_TAG, String.format("Place '%s' has likelihood: %g",
- placeLikelihood.getPlace().getName(),
- placeLikelihood.getLikelihood()));
- }
- likelyPlaces.release();
- }
- });
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- }
- }
- public enum SegmentID {
- LIST(0),
- MAP(1);
- int value;
- SegmentID(int value){
- this.value = value;
- }
- public int getValue(){
- return value;
- }
- public static SegmentID fromInteger(int x) {
- switch(x) {
- case 0:
- return LIST;
- case 1:
- return MAP;
- }
- return null;
- }
- }
- public FindMosqueFragment() {
- // Required empty public constructor
- }
- public static FindMosqueFragment newInstance() {
- return new FindMosqueFragment();
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- if (view == null) {
- view = inflater.inflate(R.layout.fragment_find_mosque, container, false);
- }
- if (mapFragment == null) {
- mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- }
- if (segmentedGroup == null) {
- segmentedGroup = (SegmentedGroup) view.findViewById(R.id.segmentedControl);
- segmentedGroup.setTintColor(Color.WHITE, Color.parseColor("#aaa194"));
- segmentedGroup.setOnCheckedChangeListener(this);
- }
- if (listButton == null) {
- listButton = (RadioButton) view.findViewById(R.id.listButton);
- listButton.setEnabled(true);
- listButton.setClickable(true);
- listButton.setId(SegmentID.LIST.getValue());
- }
- if (mapButton == null) {
- mapButton = (RadioButton) view.findViewById(R.id.mapButton);
- mapButton.setId(SegmentID.MAP.getValue());
- mapButton.setEnabled(true);
- mapButton.setClickable(true);
- }
- return view;
- }
- public void onButtonPressed(Uri uri) {
- if (mListener != null) {
- mListener.onFragmentInteraction(uri);
- }
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- if (activity instanceof OnFragmentInteractionListener)
- mListener = (OnFragmentInteractionListener) activity;
- }
- @Override
- public void onDetach() {
- mListener = null;
- super.onDetach();
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- UiSettings uiSettings = googleMap.getUiSettings();
- uiSettings.setMyLocationButtonEnabled(true);
- uiSettings.setCompassEnabled(true);
- uiSettings.setMapToolbarEnabled(true);
- uiSettings.setMyLocationButtonEnabled(true);
- this.googleMap = googleMap;
- googleMap.setIndoorEnabled(false);
- googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
- googleMap.setMyLocationEnabled(true);
- googleMap.setOnMyLocationChangeListener(new MyGoogleMapOnMyLocationListener());
- }
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- Log.d(LOG_TAG, "Checked item id: " + checkedId);
- final SegmentID si = SegmentID.fromInteger(checkedId);
- switch (si) {
- case LIST:
- mapButton.setChecked(false);
- listButton.setChecked(true);
- break;
- case MAP:
- mapButton.setChecked(true);
- listButton.setChecked(false);
- break;
- }
- }
- public interface OnFragmentInteractionListener {
- public void onFragmentInteraction(Uri uri);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement