Overview

With the HUAWEI Site SDK, your app can provide users with convenient yet secure access to diverse, place-related services. HUAWEI Site Kit provides the following core capabilities for you to quickly build apps with which your users can explore the world around them:

What You Will Create

In this codelab, you will create a place-based app, for example:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

To integrate HUAWEI Site Kit, you must complete the following preparations:

For details, please refer to Preparations for Integrating HUAWEI HMS Core.

1.Add the AppGallery Connect configuration file of your app.

(a) Download the agconnect-services.json file of your app.

(b) Add agconnect-services.json to the app directory in your project.

2.Add build dependencies.

(a) Open the build.gradle file in the app directory.

(b) Add build dependencies in the dependencies section.

dependencies { implementation 'com.huawei.hms:site:{version}' }

(c) Add apply plugin: 'com.huawei.agconnect' to the file header.

  1. Add EditText (for entering search keywords), TextView (for displaying the search results), and other components to the layout.
    <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="bottom" android:background="#D3D3D3" android:gravity="center_vertical" android:paddingLeft="5dp" android:text="Parameters" android:textSize="16sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Query: " /> <EditText android:id="@+id/edit_text_text_search_query" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:autofillHints="" android:hint="" android:imeOptions="actionGo" android:inputType="text" android:text="Eiffel Tower" /> </LinearLayout> <Button android:id="@+id/button_text_search" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="search" android:text="Search" android:textAllCaps="false" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="bottom" android:background="#D3D3D3" android:gravity="center_vertical" android:paddingLeft="5dp" android:text="Result" android:textSize="16sp" /> <TextView android:id="@+id/response_text_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" /> </LinearLayout> </ScrollView>
  2. Declare the SearchService object and call SearchServiceFactory using the onCreate method to instantiate the object. To create a SearchService instance, you need to pass the Context and API key parameters. It is recommended that the Context parameter be of the Activity type. Otherwise, no update wizard will be displayed when HMS Core (APK) needs to be updated. The API key is generated together with the agconnect-services.json file when you create an app in AppGallery Connect. Note that the API key must be encoded using encodeURI.
    package com.huawei.codelab.sitecodelab; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; import com.huawei.hms.site.api.SearchResultListener; import com.huawei.hms.site.api.SearchService; import com.huawei.hms.site.api.SearchServiceFactory; import com.huawei.hms.site.api.model.AddressDetail; import com.huawei.hms.site.api.model.SearchStatus; import com.huawei.hms.site.api.model.Site; import com.huawei.hms.site.api.model.TextSearchRequest; import com.huawei.hms.site.api.model.TextSearchResponse; import androidx.appcompat.app.AppCompatActivity; /** * site activity entrance class * */ public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private SearchService searchService; TextView resultTextView; EditText queryInput; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { searchService = SearchServiceFactory.create(this, URLEncoder.encode("your_api_key", "utf-8")); } catch (UnsupportedEncodingException e) { Log.e(TAG, "encode apikey error"); } queryInput = findViewById(R.id.edit_text_text_search_query); resultTextView = findViewById(R.id.response_text_search); } }
  3. Create a TextSearchRequest object, which is the place search request body. In the object, the query parameter is mandatory, and other parameters are optional:
    • query: search keyword.
    • location: longitude and latitude to which search results need to be biased.
    • radius: search radius, in meters. The value ranges from 1 to 50000. The default value is 50000.
    • poiType: POI type. The value range is the same as that of LocationType.
    • HwPoiType: Huawei POI type. This parameter is recommended. The value range is the same as that of HwLocationType.
    • countryCode: code of the country where places are searched. The country code must comply with the ISO 3166-1 alpha-2 standard.
    • language: language in which search results are displayed. For details about the value range, please refer to language codes in LanguageMapping. If this parameter is not passed, the language of the query field is used in priority. If the field language is unavailable, the local language will be used.
    • pageSize: number of records on each page. The value ranges from 1 to 20. The default value is 20.
    • pageIndex: current page number. The value ranges from 1 to 60. The default value is 1.
    TextSearchRequest textSearchRequest = new TextSearchRequest(); textSearchRequest.setHwPoiType(HwLocationType.TOWER); textSearchRequest.setQuery(queryInput.getText().toString());
  4. Pass the created TextSearchRequest and SearchResultListener to the textSearch method to call it, obtain the search result from onSearchResult, and display places in the search result.
    public void search(View view) { TextSearchRequest textSearchRequest = new TextSearchRequest(); textSearchRequest.setQuery(queryInput.getText().toString()); textSearchRequest.setHwPoiType(HwLocationType.TOWER); searchService.textSearch(textSearchRequest, new SearchResultListener<TextSearchResponse>() { @Override public void onSearchResult(TextSearchResponse textSearchResponse) { StringBuilder response = new StringBuilder("\n"); response.append("success\n"); int count = 1; AddressDetail addressDetail; for (Site site : textSearchResponse.getSites()) { addressDetail = site.getAddress(); response.append(String.format( "[%s] name: %s, formatAddress: %s, country: %s, countryCode: %s \r\n", "" + (count++), site.getName(), site.getFormatAddress(), (addressDetail == null ? "" : addressDetail.getCountry()), (addressDetail == null ? "" : addressDetail.getCountryCode()))); } Log.d(TAG, "search result is : " + response); resultTextView.setText(response.toString()); } @Override public void onSearchError(SearchStatus searchStatus) { Log.e(TAG, "onSearchError is: " + searchStatus.getErrorCode()); } }); }

Building, Loading, and Debugging the App

Open Gradle built in Android Studio and double-click installDebug to install the developed app.

Preparing for the Debugging

Open the developed app, tap Search to search for places based on the preset search keyword Eiffel Tower, and check whether the search results can be properly displayed.

Well done. You have successfully completed this codelab and learned how to:

You can click here and view the HUAWEI Site Kit Development Guide.

You can click the button below to download the source code.

Download source code

Code copied