Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package my.app.trendcell.com.jayagrocer;
- import android.content.Context;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.AdapterView;
- import android.widget.AutoCompleteTextView;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.common.api.PendingResult;
- import com.google.android.gms.common.api.ResultCallback;
- import com.google.android.gms.location.places.AutocompletePrediction;
- import com.google.android.gms.location.places.Place;
- import com.google.android.gms.location.places.PlaceBuffer;
- import com.google.android.gms.location.places.Places;
- import com.google.android.gms.maps.model.LatLng;
- import com.google.android.gms.maps.model.LatLngBounds;
- import my.app.trendcell.com.jayagrocer.Utils.PlaceAutocompleteAdapter;
- import my.app.trendcell.com.jayagrocer.Utils.Utils;
- public class PlaceAutoCompleteActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{
- private LinearLayout emptyResultPlaceholder, labelPromptPlaceholder;
- private static final String TAG = "AutoComplete";
- protected GoogleApiClient mGoogleApiClient;
- private PlaceAutocompleteAdapter mAdapter;
- private AutoCompleteTextView mAutocompleteView;
- private static final LatLngBounds BOUNDS_GREATER_MALAYSIA = new LatLngBounds(
- new LatLng(1.274900, 100.112386), new LatLng(6.643370, 105.232009));
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_place_auto_complete);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .enableAutoManage(this, 0 /* clientId */, this)
- .addApi(Places.GEO_DATA_API)
- .build();
- setToolbarAction(toolbar);
- Utils.setTaskBarColored(this, getResources().getColor(R.color.green_bg));
- initView();
- }
- private void initView(){
- emptyResultPlaceholder = (LinearLayout)findViewById(R.id.emptyResultPlaceholder);
- labelPromptPlaceholder = (LinearLayout)findViewById(R.id.labelPromptPlaceholder);
- }
- private void setToolbarAction(Toolbar toolbar) {
- ImageButton backBtn = (ImageButton) toolbar.findViewById(R.id.backBtn);
- backBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- mAutocompleteView = (AutoCompleteTextView)
- findViewById(R.id.autocomplete_places);
- mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);
- mAdapter = new PlaceAutocompleteAdapter(this, mGoogleApiClient, BOUNDS_GREATER_MALAYSIA,
- null);
- mAutocompleteView.setAdapter(mAdapter);
- }
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- // SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment)
- // getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
- //
- // autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
- // @Override
- // public void onPlaceSelected(final Place place) {
- // // TODO: Get info about the selected place.
- // Log.i("Autocomplete", "Place: " + place.getName() + " " + place.getLatLng().latitude + "," + place.getLatLng().longitude);
- // if (place != null) {
- // new Handler().postDelayed(new Runnable() {
- // @Override
- // public void run() {
- // Location location = new Location(LocationManager.GPS_PROVIDER);
- // location.setLatitude(place.getLatLng().latitude);
- // location.setLongitude(place.getLatLng().longitude);
- //
- // APiCall.getOutletListingAPI(PlaceAutoCompleteActivity.this, MainApplication.getInstance().getAccessToken(), 0, 20, APiCall.SORT_BY_LATEST, location);
- // }
- // }, 500);
- // }
- // }
- //
- // @Override
- // public void onError(Status status) {
- // // TODO: Handle the error.
- // Log.i("Autocomplete", "An error occurred: " + status);
- // }
- // });
- }
- public void setData(boolean isSuccess){
- if(isSuccess){
- if(emptyResultPlaceholder.getVisibility() == View.VISIBLE)
- emptyResultPlaceholder.setVisibility(View.GONE);
- }
- else{
- if(emptyResultPlaceholder.getVisibility() == View.GONE)
- emptyResultPlaceholder.setVisibility(View.VISIBLE);
- }
- labelPromptPlaceholder.setVisibility(View.GONE);
- }
- //Autocomplete buffer
- private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
- = new ResultCallback<PlaceBuffer>() {
- @Override
- public void onResult(PlaceBuffer places) {
- if (!places.getStatus().isSuccess()) {
- // Request did not complete successfully
- Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());
- places.release();
- return;
- }
- // Get the Place object from the buffer.
- final Place place = places.get(0);
- Log.i(TAG, "Place details received: " + place.getName()+" , "+place.getAddress()+" , "+place.getLocale());
- //collapse keyboard
- InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(mAutocompleteView.getWindowToken(), 0);
- Location location = new Location(LocationManager.GPS_PROVIDER);
- location.setLatitude(place.getLatLng().latitude);
- location.setLongitude(place.getLatLng().longitude);
- //APiCall.getOutletListingAPI(PlaceAutoCompleteActivity.this, MainApplication.getInstance().getAccessToken(), APiCall.SEARCH_TYPE_MANUAL, 0, 20, APiCall.SORT_BY_LATEST, location);
- places.release();
- }
- };
- private AdapterView.OnItemClickListener mAutocompleteClickListener
- = new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- /*
- Retrieve the place ID of the selected item from the Adapter.
- The adapter stores each Place suggestion in a AutocompletePrediction from which we
- read the place ID and title.
- */
- final AutocompletePrediction item = mAdapter.getItem(position);
- final String placeId = item.getPlaceId();
- final CharSequence primaryText = item.getPrimaryText(null);
- Log.i(TAG, "Autocomplete item selected: " + primaryText);
- /*
- Issue a request to the Places Geo Data API to retrieve a Place object with additional
- details about the place.
- */
- PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
- .getPlaceById(mGoogleApiClient, placeId);
- placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
- Log.i(TAG, "Called getPlaceById to get Place details for " + placeId);
- }
- };
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- Log.e("GoogleAPI", "onConnectionFailed: ConnectionResult.getErrorCode() = "
- + connectionResult.getErrorCode());
- // TODO(Developer): Check error code and notify the user of error state and resolution.
- Toast.makeText(this,
- "Could not connect to Google API Client: Error " + connectionResult.getErrorCode(),
- Toast.LENGTH_SHORT).show();
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
- // do something on back.
- finish();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement