Advertisement
vergepuppeter

PlaceAutoComplete

Mar 2nd, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.84 KB | None | 0 0
  1. package my.app.trendcell.com.jayagrocer;
  2.  
  3. import android.content.Context;
  4. import android.location.Location;
  5. import android.location.LocationManager;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.util.Log;
  10. import android.view.KeyEvent;
  11. import android.view.View;
  12. import android.view.inputmethod.InputMethodManager;
  13. import android.widget.AdapterView;
  14. import android.widget.AutoCompleteTextView;
  15. import android.widget.ImageButton;
  16. import android.widget.LinearLayout;
  17. import android.widget.Toast;
  18.  
  19. import com.google.android.gms.common.ConnectionResult;
  20. import com.google.android.gms.common.api.GoogleApiClient;
  21. import com.google.android.gms.common.api.PendingResult;
  22. import com.google.android.gms.common.api.ResultCallback;
  23. import com.google.android.gms.location.places.AutocompletePrediction;
  24. import com.google.android.gms.location.places.Place;
  25. import com.google.android.gms.location.places.PlaceBuffer;
  26. import com.google.android.gms.location.places.Places;
  27. import com.google.android.gms.maps.model.LatLng;
  28. import com.google.android.gms.maps.model.LatLngBounds;
  29.  
  30. import my.app.trendcell.com.jayagrocer.Utils.PlaceAutocompleteAdapter;
  31. import my.app.trendcell.com.jayagrocer.Utils.Utils;
  32.  
  33. public class PlaceAutoCompleteActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{
  34.  
  35.     private LinearLayout emptyResultPlaceholder, labelPromptPlaceholder;
  36.     private static final String TAG = "AutoComplete";
  37.     protected GoogleApiClient mGoogleApiClient;
  38.     private PlaceAutocompleteAdapter mAdapter;
  39.     private AutoCompleteTextView mAutocompleteView;
  40.     private static final LatLngBounds BOUNDS_GREATER_MALAYSIA = new LatLngBounds(
  41.             new LatLng(1.274900, 100.112386), new LatLng(6.643370, 105.232009));
  42.  
  43.     @Override
  44.     protected void onCreate(Bundle savedInstanceState) {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.activity_place_auto_complete);
  47.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  48.         setSupportActionBar(toolbar);
  49.  
  50.         mGoogleApiClient = new GoogleApiClient.Builder(this)
  51.                 .enableAutoManage(this, 0 /* clientId */, this)
  52.                 .addApi(Places.GEO_DATA_API)
  53.                 .build();
  54.  
  55.         setToolbarAction(toolbar);
  56.         Utils.setTaskBarColored(this, getResources().getColor(R.color.green_bg));
  57.         initView();
  58.     }
  59.  
  60.     private void initView(){
  61.         emptyResultPlaceholder = (LinearLayout)findViewById(R.id.emptyResultPlaceholder);
  62.         labelPromptPlaceholder = (LinearLayout)findViewById(R.id.labelPromptPlaceholder);
  63.     }
  64.  
  65.     private void setToolbarAction(Toolbar toolbar) {
  66.         ImageButton backBtn = (ImageButton) toolbar.findViewById(R.id.backBtn);
  67.         backBtn.setOnClickListener(new View.OnClickListener() {
  68.             @Override
  69.             public void onClick(View v) {
  70.                 finish();
  71.             }
  72.         });
  73.  
  74.         mAutocompleteView = (AutoCompleteTextView)
  75.                 findViewById(R.id.autocomplete_places);
  76.         mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);
  77.         mAdapter = new PlaceAutocompleteAdapter(this, mGoogleApiClient, BOUNDS_GREATER_MALAYSIA,
  78.                 null);
  79.         mAutocompleteView.setAdapter(mAdapter);
  80.     }
  81.  
  82.     @Override
  83.     protected void onPostCreate(Bundle savedInstanceState) {
  84.         super.onPostCreate(savedInstanceState);
  85.  
  86. //        SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment)
  87. //                getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
  88. //
  89. //        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
  90. //            @Override
  91. //            public void onPlaceSelected(final Place place) {
  92. //                // TODO: Get info about the selected place.
  93. //                Log.i("Autocomplete", "Place: " + place.getName() + " " + place.getLatLng().latitude + "," + place.getLatLng().longitude);
  94. //                if (place != null) {
  95. //                    new Handler().postDelayed(new Runnable() {
  96. //                        @Override
  97. //                        public void run() {
  98. //                            Location location = new Location(LocationManager.GPS_PROVIDER);
  99. //                            location.setLatitude(place.getLatLng().latitude);
  100. //                            location.setLongitude(place.getLatLng().longitude);
  101. //
  102. //                            APiCall.getOutletListingAPI(PlaceAutoCompleteActivity.this, MainApplication.getInstance().getAccessToken(), 0, 20, APiCall.SORT_BY_LATEST, location);
  103. //                        }
  104. //                    }, 500);
  105. //                }
  106. //            }
  107. //
  108. //            @Override
  109. //            public void onError(Status status) {
  110. //                // TODO: Handle the error.
  111. //                Log.i("Autocomplete", "An error occurred: " + status);
  112. //            }
  113. //        });
  114.     }
  115.  
  116.     public void setData(boolean isSuccess){
  117.         if(isSuccess){
  118.             if(emptyResultPlaceholder.getVisibility() == View.VISIBLE)
  119.                 emptyResultPlaceholder.setVisibility(View.GONE);
  120.         }
  121.         else{
  122.             if(emptyResultPlaceholder.getVisibility() == View.GONE)
  123.                 emptyResultPlaceholder.setVisibility(View.VISIBLE);
  124.         }
  125.  
  126.         labelPromptPlaceholder.setVisibility(View.GONE);
  127.     }
  128.  
  129.     //Autocomplete buffer
  130.     private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
  131.             = new ResultCallback<PlaceBuffer>() {
  132.         @Override
  133.         public void onResult(PlaceBuffer places) {
  134.             if (!places.getStatus().isSuccess()) {
  135.                 // Request did not complete successfully
  136.                 Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());
  137.                 places.release();
  138.                 return;
  139.             }
  140.  
  141.             // Get the Place object from the buffer.
  142.             final Place place = places.get(0);
  143.             Log.i(TAG, "Place details received: " + place.getName()+" , "+place.getAddress()+" , "+place.getLocale());
  144.  
  145.             //collapse keyboard
  146.             InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  147.             imm.hideSoftInputFromWindow(mAutocompleteView.getWindowToken(), 0);
  148.  
  149.             Location location = new Location(LocationManager.GPS_PROVIDER);
  150.             location.setLatitude(place.getLatLng().latitude);
  151.             location.setLongitude(place.getLatLng().longitude);
  152.  
  153.             //APiCall.getOutletListingAPI(PlaceAutoCompleteActivity.this, MainApplication.getInstance().getAccessToken(), APiCall.SEARCH_TYPE_MANUAL, 0, 20, APiCall.SORT_BY_LATEST, location);
  154.  
  155.             places.release();
  156.         }
  157.     };
  158.  
  159.     private AdapterView.OnItemClickListener mAutocompleteClickListener
  160.             = new AdapterView.OnItemClickListener() {
  161.         @Override
  162.         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  163.             /*
  164.              Retrieve the place ID of the selected item from the Adapter.
  165.              The adapter stores each Place suggestion in a AutocompletePrediction from which we
  166.              read the place ID and title.
  167.               */
  168.             final AutocompletePrediction item = mAdapter.getItem(position);
  169.             final String placeId = item.getPlaceId();
  170.             final CharSequence primaryText = item.getPrimaryText(null);
  171.  
  172.             Log.i(TAG, "Autocomplete item selected: " + primaryText);
  173.  
  174.             /*
  175.              Issue a request to the Places Geo Data API to retrieve a Place object with additional
  176.              details about the place.
  177.               */
  178.             PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
  179.                     .getPlaceById(mGoogleApiClient, placeId);
  180.             placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
  181.  
  182.             Log.i(TAG, "Called getPlaceById to get Place details for " + placeId);
  183.         }
  184.     };
  185.  
  186.     @Override
  187.     public void onConnectionFailed(ConnectionResult connectionResult) {
  188.  
  189.         Log.e("GoogleAPI", "onConnectionFailed: ConnectionResult.getErrorCode() = "
  190.                 + connectionResult.getErrorCode());
  191.  
  192.         // TODO(Developer): Check error code and notify the user of error state and resolution.
  193.         Toast.makeText(this,
  194.                 "Could not connect to Google API Client: Error " + connectionResult.getErrorCode(),
  195.                 Toast.LENGTH_SHORT).show();
  196.     }
  197.  
  198.     @Override
  199.     public boolean onKeyDown(int keyCode, KeyEvent event)  {
  200.         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
  201.             // do something on back.
  202.             finish();
  203.             return true;
  204.         }
  205.         return super.onKeyDown(keyCode, event);
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement