[Huawei Developers] Freemind R:
When users search for places, they may not specify exactly what aspect of that place they are interested in. Search results should include information about both parent nodes (the place itself) and child nodes (related information), because it makes it easier for users to find the information they're looking for. For example, if a user searches for an airport, your app can also return information about child nodes, such as terminals, parking lots, and entrances and exits. This enables your app to provide more scenario-specific results, making it easier for users to explore their surroundings.

This post shows you how you can integrate Site Kit into your app and return information about both parent and child nodes for the places your users search for.

1. Preparations

Before you get started, there is a few preparations you'll need to make. First, make sure that you have configured the Maven repository address of the Site SDK in your project, and integrated the Site SDK.

1.1 Configure the Maven repository address in the project-level build.gradle file.

<p style="line-height: 1.5em;">buildscript {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
// Add a dependency on the AppGallery Connect plugin.
dependencies {
classpath "com.android.tools.build:gradle:3.3.2"
}
}
</p>

<p style="line-height: 1.5em;">allprojects {
repositories {
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
}
</p>

1.2 Add a dependency on the Site SDK in the build.gradle file in the app directory.

<p style="line-height: 1.5em;">dependencies {
implementation 'com.huawei.hms:site:4.0.0.202'
}
</p>

2. Development Procedure

2.1 Create a SearchService object.

<p style="line-height: 1.5em;">SearchService searchService = SearchServiceFactory.create(this, Utils.getApiKey());
</p>

2.2 Create the SearchResultListener class so your app can process the search result.

The SearchResultListener class implements the SearchResultListener<TextSearchResponse> method. The onSearchResult(TextSearchResponse results) method in this class is used to obtain the search result and implement the specific service.

<p style="line-height: 1.5em;">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) {
// Handle the search result as needed.
....

// Obtain information about child nodes.
if ((site.getPoi() != null)) {
ChildrenNode[] childrenNodes = poi.getChildrenNodes();
// Handle the information as needed.
....

}
}
}

@Override
public void onSearchError(SearchStatus status) {
resultTextView.setText("Error : " + status.getErrorCode() + " " + status.getErrorMessage());
}
}; </p>

2.3 Create the TextSearchRequest class and set the request parameters.

<p style="line-height: 1.5em;">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 obtain child node information.
request.setChildren(true);
</p>

2.4 Set a request result handler and bind it with the request.

<p style="line-height: 1.5em;">searchService.textSearch(request, resultListener);
</p>

Once you have completed the steps above, your app will be able to return information about both the parent node and its child nodes. This attachment shows how the search results will look:

You can find the source code on GitHub.

For more details, you can go to:

l Our official website

l Our development guide

l Reddit to join our developer discussion

l GitHub to download demos and sample codes

l Stack Overflow to solve any integration problems