使用Google Places Android API时,API通常会获取用户的位置并返回附近地点的列表。如何将此列表仅限于健身房?虽然网络解决方案很容易获得,但我似乎无法找到如何为Android做到这一点。
这是我目前的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
ArrayList<Integer> types = new ArrayList<>();
types.add(Places.TYPE_GYM);
for(PlaceLikelihood likelihood : result) {
if(hasMatchingType(result.getPlace(), types)) {
//This is a gym, do whatever you need to here
}
}
}
private boolean hasMatchingType (Place place, List < Integer > allowedTypes){
List<Integer> types = place.getPlaceTypes();
for (int type : types) {
if (allowedTypes.contains(type)) {
return true;
}
}
return false;
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}