DEV Community

HarmonyOS
HarmonyOS

Posted on

The search results using the "poi" keyword are inconsistent with those from Petal Maps.

Read the original article:The search results using the "poi" keyword are inconsistent with those from Petal Maps.

Question

When calling searchByText to search for locations, the returned data differs significantly from the results returned by Petal Map. How can we return results that are the same as Petal Map?

Short Answer

SearchByTextParams: SearchByTextParams defines the parameters for the search keyword. When searching with the Petal Map app, it automatically retrieves the current latitude and longitude parameters and passes them to SearchByTextParams. Therefore, when calling the API to search for locations, you need to obtain the current latitude and longitude information through a web map or other methods and pass it to SearchByTextParams. This way, you can get the same search results as Petal Map. Example code is as follows:

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

@Entry
@Component
struct Index {
  async poiSearch(){
    let params: site.SearchByTextParams = {
      // Specify the keyword, replace xxx with the specific address
      query: "Rabbit Head",
      // Insert the latitude and longitude coordinates of the location
      location: {
        latitude: 1.000,
        longitude: 2.000
      },
      // Specify the geographical range radius, in meters. If limited to 1000, it will only display search results within 1 kilometer
      radius: 10000,
      language: "zh"
    };
    // Return the keyword search results
    const result = await site.searchByText(params);
    console.info("Search result:", JSON.stringify(result));
  }
  build() {
    Column() {
      Button('click').onClick(async ()=>{
        await this.poiSearch()
      })
    }
    .width("100%")
    .height("100%")
    .justifyContent(FlexAlign.Center)
  }
}
Enter fullscreen mode Exit fullscreen mode

Applicable Scenarios

  1. Location-Based Services (LBS) Development:
    • Developers working on location-based services or applications that require accurate location search functionalities can use this approach to ensure their search results align with popular map applications like Petal Map.
  2. Integration with Web Maps:
    • When integrating with web maps or other mapping services, developers can use the provided method to fetch the current latitude and longitude and pass these values to SearchByTextParams to get consistent search results.
  3. Enhancing User Experience:
    • Improving the user experience by providing location search results that are consistent with what users expect from well-known map applications, ensuring reliability and trust in the application.
  4. Custom Map Applications:
    • Developers creating custom map applications or integrating map functionalities into existing applications can use this method to ensure their search functionalities are robust and accurate.
  5. Geolocation-Based Features:
    • Implementing geolocation-based features such as nearby places, location-based recommendations, or location-based advertisements where accurate and consistent search results are crucial.
  6. Testing and Debugging:
    • During the testing and debugging phase, developers can use this method to compare their application's search results with those of Petal Map to ensure consistency and accuracy.
  7. API Integration:
    • When integrating with third-party APIs that require location parameters, this method can be used to ensure that the location data is correctly formatted and passed to the API for accurate results.
  8. Cross-Platform Development:
    • For developers working on cross-platform applications, this method can be adapted to ensure consistent location search results across different platforms and devices.

Reference Link

https://developer.huawei.com/consumer/en/doc/harmonyos-guides/map-kit-guide

Written by Mehmet Algul

Top comments (0)