我一直在将mapbox添加到我的应用程序中,我让地理编码器使用自动完成功能进行搜索。我无法想象如何搜索类别。该功能尚未推出吗? API文档将其显示为"预览"我也找到了这个链接。 https://github.com/mapbox/MapboxGeocoder.swift/issues/119
这就是我想要做的事情
// 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();
}
}
我把它换成了
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();
}
}
这给了我餐馆,但不是我附近的餐馆。 但如果我改变了这个
.setLocation("restaurant")
到此
.setLocation(constraint.toString())
我可以使用自动完成功能按名称搜索餐厅,我会找到我要找的任何一家餐馆。
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);
}
使用此项我设置标记的标题以显示所选餐厅的属性,因此我可以检查它所属的类别,以确保我在搜索&#34;时搜索正确的类别。餐厅&#34 ;.
如果属于多个类别,是否需要设置多个类别?如果我需要这样做,我该怎么做?