I been adding mapbox to my app and I got the geocoder to search using auto complete. I can't figure how to search categories tho. Is that feature not yet available? The API documents show it as "preview" and I also found this link. https://github.com/mapbox/MapboxGeocoder.swift/issues/119
This what I'm trying to do
// The geocoder client
MapboxGeocoder client = new MapboxGeocoder.Builder()
.setAccessToken(Constants.MAPBOX_ACCESS_TOKEN)
.setLocation("restaurant")
.setType(GeocoderCriteria.TYPE_POI)
.build();
Response<GeocoderResponse> response;
try {
response = client.execute();
} catch (IOException e) {
e.printStackTrace();
return results;
}
features = response.body().getFeatures();
results.values = features;
results.count = features.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
features = (List<GeocoderFeature>) results.values;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
I switched it to this
MapboxGeocoding client = new MapboxGeocoding.Builder()
.setAccessToken("pk.eyJ1IjoiamViMTkyMDA0IiwiYSI6ImNpbWNyODZyaDAwMmZ1MWx2dHdzcHQ5M2EifQ.IZsMnB3wOYFIaX1A5sy7Mw")
.setLocation("restaurant")
.setCountry("us")
.setGeocodingType(GeocodingCriteria.TYPE_POI_LANDMARK)
.setProximity(position)
.build();
Response<GeocodingResponse> response;
try {
response = client.executeCall();
} catch (IOException e) {
e.printStackTrace();
return results;
}
features = (List<CarmenFeature>) response.body().getFeatures();
results.values = features;
results.count = features.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
features = (List<CarmenFeature>) results.values;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
This gives me restaurants but not the ones in my proximity. But if I change this
.setLocation("restaurant")
To this
.setLocation(constraint.toString())
I can use the auto complete to search for the restaurant by name and I'll find whichever restaurant I'm looking for.
final GeocoderAdapter adapter = new GeocoderAdapter(this);
autocomplete = (AutoCompleteTextView) findViewById(R.id.query);
autocomplete.setLines(1);
autocomplete.setAdapter(adapter);
autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CarmenFeature result = adapter.getItem(position);
autocomplete.setText(result.getText());
updateMap(result.asPosition().getLatitude(), result.asPosition().getLongitude(), result.getProperties().toString());
}
});
// Add clear button to autocomplete
final Drawable imgClearButton = getResources().getDrawable(R.drawable.zoom_out);
autocomplete.setCompoundDrawablesWithIntrinsicBounds(null, null, imgClearButton, null);
autocomplete.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
AutoCompleteTextView et = (AutoCompleteTextView) v;
if (et.getCompoundDrawables()[2] == null)
return false;
if (event.getAction() != MotionEvent.ACTION_UP)
return false;
if (event.getX() > et.getWidth() - et.getPaddingRight() - imgClearButton.getIntrinsicWidth()) {
autocomplete.setText("");
}
return false;
}
});
private void updateMap(double latitude, double longitude, String title) {
// Marker
map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(title));
// Animate map
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude))
.zoom(13)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 5000, null);
}
Using this I set the title of the marker to show properties of the selected restaurant and so I could check the category it fall under to make sure I'm searching the right category when trying to search for "restaurant".
Do I need to set multiple categories if it fall under multiple categories? How do I do that if I need to do that?