将 Algolia 中的建议限制为邮政编码

Restrict suggestions in Algolia to postcode

在 Algolia Places 中,我想将街道建议限制为特定邮政编码。但是我似乎无法让它工作。我以为这可能是它的完成方式,但它不起作用。

const optionsStreet = {
    appId: '...',
    apiKey: '...',
    container: document.querySelector('#address'),
    templates: {
        value: function (suggestion) {
            return suggestion.name;
        }
    }
};

const configStreet = {
    language: 'de', // Receives results in German
    postcodes: ['12345'],
    type: 'address'
};


const placesStreet = places(optionsStreet).configure(configStreet);

placesStreet.on('change', function resultSelected(e) {
    document.querySelector('#address').value = e.suggestion.name || '';
});

没有简单的方法可以做你想做的事。
您可能想要提出功能请求!

同时...

第一个选项

如果这是您可以获得的一些数据,请使用 insidePolygon 选项将搜索限制在您感兴趣的邮政编码所代表的地理区域。
请注意,您 可以 过滤 multiple disjoint polygons

该选项设置起来有点麻烦,但会给您最好的结果,因为它确保您只会从您想要的区域获得命中。

第二个选项

您也可以使用 that part of the code:

来分叉项目和 fiddle
      .then(content => {
        const hits = content.hits.map((hit, hitIndex) =>
          formatHit({
            formatInputValue: controls.formatInputValue,
            hit,
            hitIndex,
            query,
            rawAnswer: content,
          })
        );

        controls.onHits({
          hits,
          query,
          rawAnswer: content,
        });

        return hits;
      })

过滤 content.hits 仅匹配您的邮政编码的匹配项,您将获得过滤后的建议。

两条建议:

  1. 增加 hitsPerPage 设置,因为由于邮政编码过滤器,您将从命中中删除许多结果:
const placesStreet = places(options).configure({
  ...config,
  computeQueryParams(query) {
    return {...query, hitsPerPage: 100};
  }
});

您需要根据需要微调 100

  1. 使用 arountLatLng 配置指定您要定位的邮政编码区域内的一个点,以最大限度地提高具有正确邮政编码的命中数。