The widget is a search component of the built-in place search suggestion feature. When a user enters a keyword in the search box, the widget displays a list of suggested places to the user. If the user taps a place in the list, your app will receive a Site object and use it to obtain details about this place.
You can use the widget in your app using either of the following methods:
You need to add the build dependency implementation 'androidx.recyclerview:recyclerview:1.1.0' to the dependencies block in the app-level build.gradle file. The API key is generated when you create an app on HUAWEI Developers. Note that you need to call the URLEncoder.encode("Your apiKey", "UTF-8") method to encode the API key using encodeURI.
To add the widget to your app using SearchFragment, perform the following steps:
<fragment
android:id="@+id/widget_fragment"
android:name="com.huawei.hms.site.widget.SearchFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> Java SearchFragment fragment = (SearchFragment) getSupportFragmentManager().findFragmentById(R.id.widget_fragment); // Set the API key of SearchFragment. fragment.setApiKey("API key"); fragment.setOnSiteSelectedListener(new SiteSelectionListener() { @Override public void onSiteSelected(Site data) { Toast.makeText(getApplication(), data.getName(), Toast.LENGTH_LONG).show(); } @Override public void onError(SearchStatus status) { Toast .makeText(getApplication(), status.getErrorCode() + "\n" + status.getErrorMessage(), Toast.LENGTH_LONG) .show(); } });
Kotlin val fragment: SearchFragment = supportFragmentManager.findFragmentById(R.id.widget_fragment) as SearchFragment // Set the API key of SearchFragment. fragment.setApiKey("API key") fragment.setOnSiteSelectedListener(object : SiteSelectionListener { override fun onSiteSelected(data: Site) { Toast.makeText(application, data.getName(), Toast.LENGTH_LONG).show() } override fun onError(status: SearchStatus) { Toast .makeText(application, status.getErrorCode() + "\n" + status.getErrorMessage(), Toast.LENGTH_LONG) .show() } })
Figure 1 and Figure 2 show the search box and search result, respectively.
To start the widget using Intent, perform the following steps:
Java private SearchIntent searchIntent; searchIntent = new SearchIntent(); searchIntent.setApiKey("API key");
Kotlin private val searchIntent = SearchIntent() searchIntent.setApiKey("API key")
Java
Intent intent = searchIntent.getIntent(this);
startActivityForResult(intent, SearchIntent.SEARCH_REQUEST_CODE); Kotlin
val intent = searchIntent.getIntent(this)
startActivityForResult(intent, SearchIntent.SEARCH_REQUEST_CODE) Java
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (SearchIntent.SEARCH_REQUEST_CODE == requestCode) {
if (SearchIntent.isSuccess(resultCode)) {
Site site;
site = searchIntent.getSiteFromIntent(data);
Toast.makeText(getApplication(), site.getName(), Toast.LENGTH_LONG).show();
}
}
} Kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (SearchIntent.SEARCH_REQUEST_CODE == requestCode) {
if (SearchIntent.isSuccess(resultCode)) {
val site: Site = searchIntent.getSiteFromIntent(data)
Toast.makeText(application, site.getName(), Toast.LENGTH_LONG).show()
}
}
} You can use setFilter(SearchFilter) to restrict the suggested places for both SearchFragment and SearchIntent. To do so, you need to create a SearchFilter object and set restriction conditions described in the following table.
Method | Description |
|---|---|
setQuery(String query) | Sets the search keywords. |
setLocation(Coordinate location) | Sets the longitude and latitude to which search results need to be biased. |
setRadius(Integer radius) | Sets the search radius, in meters. The value ranges from 1 to 50,000. The default value is 50000. |
setBounds(CoordinateBounds bounds) | Sets the coordinate bounds to which search results need to be biased. |
setCountryCode(String countryCode) | Sets the code of the country or region where places are searched, which complies with the ISO 3166-1 alpha-2 standard. |
setLanguage(String language) | Sets the language in which search results are displayed. For details about the value range, please refer to language codes in Supported Languages. If no language is specified, English will be used. If English is unavailable, the local language will be used. |
setPoiType(List<LocationType> poiType) | Sets the POI type of the returned place. The value range is a subset of LocationType, including GEOCODE, ADDRESS, ESTABLISHMENT, REGIONS, and CITIES. |
setPoliticalView(String politicalView) | Sets the political view. CAUTION: This method has been deprecated. |
setChildren(boolean children) | Sets whether to return information about child nodes. |
setStrictBounds(Boolean strictBounds) | Sets whether to strictly restrict place search in the bounds specified by Bounds. The default value is false. |