2

我一直在将 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);
}

使用它我设置标记的标题以显示所选餐厅的属性,因此我可以检查它所属的类别,以确保在尝试搜索“餐厅”时搜索正确的类别。

如果它属于多个类别,我是否需要设置多个类别?如果我需要这样做,我该怎么做?

4

0 回答 0