目录

使用POI关键字搜索结果跟花瓣地图搜索不一致

问题现象

调用searchByText进行地点搜索的时候,返回的数据跟用花瓣地图返回的结果差异较大。如何能返回跟花瓣地图相同的结果。

解决方案

本解决方案需要开通地图服务,并在项目中进行相应配置。

SearchByTextParams:SearchByTextParams定义了搜索关键字的参数。

花瓣地图APP搜索会默认获取当前的经纬度参数并传值到SearchByTextParams。所以调用API进行地点搜索的时候,需要通过网页地图,或者其他方法获取到当前的经纬度信息,并传值到SearchByTextParams。就可以获取跟花瓣地图搜索相同的值。

import { site } from '@kit.MapKit';

@Entry
@Component
struct Index {
  async poiSearch() {
    let params: site.SearchByTextParams = {
      query: '牛肉',
      location: {
        latitude: 1.000,
        longitude: 2.000
      },

      radius: 10000,
      language: 'zh'
    };
    try {
      const result = (await site.searchByText(params)).totalCount;
      console.info('搜索结果:', JSON.stringify(result));
    } catch (error) {
      console.error(`Failed to code ${error.code},message is ${error.message}`);
    }
  }

  build() {
    Column() {
      Button('click').onClick(async () => {
        await this.poiSearch();
      });
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center);
  }
}