Android PlaceFilter usage with Google Places API

冷暖自知 提交于 2019-12-06 21:22:48

问题


I am trying to use the Google Places API and I would like to filter my search to only gym types.

I am using the code given at https://developers.google.com/places/

public void onPickButtonClick(View v) {
// Construct an intent for the place picker
try {
    PlacePicker.IntentBuilder intentBuilder =
        new PlacePicker.IntentBuilder();
    Intent intent = intentBuilder.build(this);
    // Start the intent by requesting a result,
    // identified by a request code.
    startActivityForResult(intent, REQUEST_PLACE_PICKER);

} catch (GooglePlayServicesRepairableException e) {
    // ...
} catch (GooglePlayServicesNotAvailableException e) {
    // ...
}
}

@Override
protected void onActivityResult(int requestCode,
    int resultCode, Intent data) {

if (requestCode == REQUEST_PLACE_PICKER
    && resultCode == Activity.RESULT_OK) {

    // The user has selected a place. Extract the name and address.
    final Place place = PlacePicker.getPlace(data, this);

    final CharSequence name = place.getName();
    final CharSequence address = place.getAddress();
    String attributions = PlacePicker.getAttributions(data);
    if (attributions == null) {
        attributions = "";
    }

    mViewName.setText(name);
    mViewAddress.setText(address);
    mViewAttributions.setText(Html.fromHtml(attributions));

} else {
    super.onActivityResult(requestCode, resultCode, data);
}
}

What I was trying to do was to create a filter like below and somehow add that (placeFilter) to the intent.

PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
PlaceFilter placeFilter;
ArrayList<String> restictToGyms = new ArrayList<>();
restictToGyms.add("44");
placeFilter = new PlaceFilter(false, restictToGyms);
intent.SOMEHOWADD(placeFilter);
Intent intent = intentBuilder.build(this);

But I am not sure how to add this filter to the intent. In fact I am not sure if this is the right way of doing it. Any pointers will be appreciated. Thanks!


回答1:


I've been looking to do the same thing. Unfortunately it doesn't seem possible- the PlaceFilter takes place ids, not types. The class PlaceFilter is final so it cannot be extended (I hate it when API writers make classes final, there's almost never a reason to do it). You need to actually make the call and do the filtering via a loop after the result returns.




回答2:


Refer to this https://developer.android.com/reference/com/google/android/gms/location/places/Place.html

Gym is Place.TYPE_GYM



来源:https://stackoverflow.com/questions/29584266/android-placefilter-usage-with-google-places-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!