目录

Widget

Overview

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.

Adding a SearchFragment Object to Start the Widget

To add the widget to your app using SearchFragment, perform the following steps:

  1. Add SearchFragment to the activity by adding a fragment element to the XML layout file of the activity. An example is as follows:
     <fragment
            android:id="@+id/widget_fragment"
            android:name="com.huawei.hms.site.widget.SearchFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
  2. Add a listener for SearchFragment. To do so, bind SearchFragment to the activity, and add SiteSelectionListener for listening on the list of suggested places. If a user taps a place in the list, your app will receive the Site object of this place. For the complete example, please refer to SearchFragmentActivity.java.
    The sample code is as follows:
    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.

    Figure 1 Search box
    Figure 2 Search result list

Using Intent to Start the Widget

To start the widget using Intent, perform the following steps:

  1. Create a SearchIntent object and set the API key for it.
    The sample code is as follows:
    Java
    private SearchIntent searchIntent;
    searchIntent = new SearchIntent();
    searchIntent.setApiKey("API key"); 
    Kotlin
    private val searchIntent = SearchIntent()
    searchIntent.setApiKey("API key")
  2. Create an Intent object and use startActivityForResult(Intent intend, int requestCode) to start SearchIntent.
    The sample code is as follows:
    Java
    Intent intent = searchIntent.getIntent(this);
    startActivityForResult(intent, SearchIntent.SEARCH_REQUEST_CODE);
    Kotlin
    val intent = searchIntent.getIntent(this)
    startActivityForResult(intent, SearchIntent.SEARCH_REQUEST_CODE)
  3. Override onActivityResult(int requestCode, int resultCode, Intent data) of Activity, and call the onActivityResult(requestCode, resultCode, data) method of the parent class. The Site object of the tapped place can be obtained from the returned Intent object. For the complete example, please refer to SearchIntentActivity.java.
    The sample code is as follows:
    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()
            }
        }
    }

Restricting the Suggested Places

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.