【问题标题】:Algolia - Places autocomplete using php api clientAlgolia - 使用 php api 客户端自动完成位置
【发布时间】:2019-01-15 18:24:30
【问题描述】:

我一直在使用 Algolia 自动完成 places.js 库。当您使用该库时,您会得到一个建议列表,例如

{
  "query": "pari",
  "suggestion": {
     "name": "Paris",
     "administrative": "Île-de-France",
     "country": "France",
     "countryCode": "fr",
     "type": "city",
     "latlng": {
     "lat": 48.8546,
     "lng": 2.34771
   },
  "postcode": "75000",
  "highlight": {
      "name": "<em>Pari</em>s",
      "administrative": "Île-de-France",
      "country": "France"
   },
   "value": "Paris, Île-de-France, France"
 }
}

我需要使用 php 客户端并为我自己的应用程序 api 返回一个建议列表,例如

$places = \AlgoliaSearch\Client::initPlaces();

$result = $places->search($term, [

        'type' => ['city', 'country', 'address'],
        'language' => 'en',
        'aroundLatLngViaIP' => false,

]);

dd($result);

但是,当您使用 php 客户端(请注意,我在本例中使用的是 laravel scout)时,您不会获得建议列表,即在您可以返回给最终用户的响应 - 您最终会得到以下响应?

 {
   "hits": [{
   "objectID": "145746683_7444",
      "locale_names": {
         "default": ["Paris"],
    },
   "city": {
       "default": ["Paris"],
    },
   "county": {
       "default": ["Paris"],
   },
   "administrative": ["Île-de-France"],
   "country": {
       "default": "France",
   },
   "country_code": "fr",
   "postcode": ["75000"],
   "population": 2243833,
   "_geoloc": {
       "lat": 48.8564,
       "lng": 2.3521
   },
   "_highlightResult": {
       "locale_names": {
          "default": [{
              "value": "<em>Paris</em>",
              "fullyHighlighted": true,
              "matchedWords": ["paris"],
              "matchLevel": "full"
          }]
     },

   }
 }],
 "nbHits": 1,
 "query": "Paris"
}

【问题讨论】:

    标签: algolia


    【解决方案1】:

    如果您查看Algolia Places JavaScript library,这就是他们在将其交给自动完成之前正在处理的数据:

        const name = hit.locale_names[0];
        const country = hit.country;
        const administrative =
          hit.administrative && hit.administrative[0] !== name
            ? hit.administrative[0]
            : undefined;
        const city = hit.city && hit.city[0] !== name ? hit.city[0] : undefined;
        const suburb =
          hit.suburb && hit.suburb[0] !== name ? hit.suburb[0] : undefined;
    
        const county =
          hit.county && hit.county[0] !== name ? hit.county[0] : undefined;
    
        const { postcode, highlightedPostcode } =
          hit.postcode && hit.postcode.length
            ? getBestPostcode(hit.postcode, hit._highlightResult.postcode)
            : { postcode: undefined, highlightedPostcode: undefined };
    
        const highlight = {
          name: getBestHighlightedForm(hit._highlightResult.locale_names),
          city: city
            ? getBestHighlightedForm(hit._highlightResult.city)
            : undefined,
          administrative: administrative
            ? getBestHighlightedForm(hit._highlightResult.administrative)
            : undefined,
          country: country ? hit._highlightResult.country.value : undefined,
          suburb: suburb
            ? getBestHighlightedForm(hit._highlightResult.suburb)
            : undefined,
          county: county
            ? getBestHighlightedForm(hit._highlightResult.county)
            : undefined,
          postcode: highlightedPostcode,
        };
    
        const suggestion = {
          name,
          administrative,
          county,
          city,
          suburb,
          country,
          countryCode: findCountryCode(hit._tags),
          type: findType(hit._tags),
          latlng: {
            lat: hit._geoloc.lat,
            lng: hit._geoloc.lng,
          },
          postcode,
          postcodes: hit.postcode && hit.postcode.length ? hit.postcode : undefined,
        };
    
        // this is the value to put inside the <input value=
        const value = formatInputValue(suggestion);
    

    您应该在此代码中找到您需要的所有内容,更具体地说,对于找到的地点的完整显示名称,它是由此构建的(上面代码中的 suggestion 是下面代码的渲染上下文):

     const out = `${name}${type !== 'country' && country !== undefined ? ',' : ''}
     ${city ? `${city},` : ''}
     ${administrative ? `${administrative},` : ''}
     ${country ? country : ''}`
        .replace(/\s*\n\s*/g, ' ')
        .trim();
      return out;
    
    

    (在formatInputValue.js file`中找到)

    【讨论】:

    • 这不能回答问题,最好在评论部分说明。 formatInputValue.js 文件中使用的变量不使用 PHP 客户端返回的相同数组结构
    • @AngelS.Moreno 如果您花时间查看我首先链接到的代码,它确实回答了这个问题。然后,您会意识到 formatInputValue.js 中处理的数据正是源自 OP 在他的问题中发布的内容。我更新了我的答案以包含完整的代码,使其更加独立。
    • 我的问题是发布的问题是在 PHP 中,而您的解决方案是在 JS 中。我会尝试将代码转换成PHP,看看它是否有效。
    • OP 引用了比 PHP 更多的 JSON 代码,事实证明,来自 Algolia 的库中回答问题的相关代码都在 JS 中。问题和问题不是特定于 PHP 的,而是“我如何操作客户端返回的数据”,而这正是我所展示的 :)
    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    相关资源
    最近更新 更多