When you use location search, parent-child node information can help you unlock richer location results, making your travel easier. For example, when you search the airport, you will not only return the information of the airport, but also the information of the terminal, parking lot, entrance and exit, providing users with more scenario-based location results to meet the needs of users to explore large places.

This article describes how to use Huawei Location service to search for location and return information about parent and child nodes. The following are the specific development steps

1. Preparation before development

You need to complete the necessary development preparations, and ensure that your project has configured the Maven repository address of the HMS Core SDK and completed the SDK integration of the service.

1.1 Configure maven storehouse addresses in the project-level build.gradle file.

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://developer.huawei.com/repo/'}}// Configure AGC plug-in
    dependencies {
        classpath "Com. Android. Tools. Build: gradle: 3.3.2 rainfall distribution on 10-12"}}Copy the code
allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://developer.huawei.com/repo/'}}}Copy the code

1.2 Introduce the SDK in build.gradle at the application level.

ependencies {

    implementation 'com. Huawei. HMS: site: 4.0.0.202'

}
Copy the code

2. Development steps

2.1 Creating a Search Service.

SearchService searchService = SearchServiceFactory.create(this, Utils.getApiKey());
Copy the code

2.2 Developers create query result processing classes

SearchResultListener, which implements the SearchResultListener interface. The onSearchResult(TextSearchResponse Results) method in this class is used to obtain query results and implement specific services.

SearchResultListener<TextSearchResponse> resultListener = new SearchResultListener<TextSearchResponse>() {
    @Override
    public void onSearchResult(TextSearchResponse results) {

        Log.d(TAG, "onTextSearchResult: " + results.toString());
        List<Site> siteList;
        if (results == null || results.getTotalCount() <= 0 || (siteList = results.getSites()) == null || siteList.size() <= 0) {
            resultTextView.setText("Result is Empty!");
            return;
        }

       
        for (Site site : siteList) {

            // The developer processes the data results as needed.// Child node data
                        if((site.getPoi() ! =null)) {
                ChildrenNode[] childrenNodes = poi.getChildrenNodes();

                // The developer processes the data results as needed. }}}@Override
    public void onSearchError(SearchStatus status) {
        resultTextView.setText("Error : " + status.getErrorCode() + ""+ status.getErrorMessage()); }};Copy the code

2.3 Create the TextSearchRequest class and configure the request parameters.

TextSearchRequest request = new TextSearchRequest();
String query = "Josep Tarradellas Airport";
request.setQuery(query);
Double lat = 41.300621;
Double lng = 2.0797638;
request.setLocation(new Coordinate(lat, lng));
// Set to get child node information
request.setChildren(true);
Copy the code

2.4 Set the request result processor to bind the request and result processor.

searchService.textSearch(request, resultListener);
Copy the code

In this way, the search location is returned to the parent node information, the following is the implementation of the renderings, to show the power of this function.

For more details, please refer to:

Huawei developer alliance website: developer.huawei.com/consumer/cn…

To obtain development guidance document: developer.huawei.com/consumer/cn…

Participate in developer discussion please to Reddit community: www.reddit.com/r/HMSCore/

Download the demo and sample code at Github: github.com/HMS-Core

Solve the integration problem please to Stack Overflow:stackoverflow.com/questions/t…


The original link: developer.huawei.com/consumer/cn… Author: Pepper