[Huawei Developers] berkberber:
If we briefly talk about what HMS Site Kit is, you can provide users to explore the world faster with Site Kit. You can search for locations by keywords, find places which are close to the specified coordinate point, get detailed information about a place and get suggestions for places by keyword.

Thanks to Nearby Place Search feature provided by Site Kit, we can get a list of places around based on user’s current location. While performing this search, we can configure the search results according to our application by specifying certain parameters.

First of all, I will show you how you can use Nearby Place Search feature on a function. Then we will examine these parameters and code in detail.

fun nearbySearch(coordinate: Coordinate, keyword: String, radius: Int, locationType: LocationType,
hwLocationType: HwLocationType, pageIndex: Int, pageSize: Int){
val searchService = SearchServiceFactory.create(context,
URLEncoder.encode(
"Your-API-KEY",
"utf-8"))

var request = NearbySearchRequest()

request.location = coordinate
request.query = keyword
request.radius = radius
request.hwPoiType = hwLocationType
request.poiType = locationType
request.pageIndex = pageIndex
request.pageSize = pageSize
request.language = Locale.getDefault().language // Getting system language

searchService.nearbySearch(request, object: SearchResultListener<NearbySearchResponse>{
override fun onSearchError(searchStatus: SearchStatus?) {
Log.e("SITE_KIT","${searchStatus?.errorCode} - ${searchStatus?.errorMessage}")
}

override fun onSearchResult(nearbySearchResponse: NearbySearchResponse?) {
var siteList = nearbySearchResponse?.sites
siteList?.let {
for(site in siteList){
Log.i("SITE_KIT", "Name => ${site.name}," +
"Format address => ${site.formatAddress}, " +
"Coordinate => ${site.location.lat} - ${site.location.lng}, " +
"Phone => ${site.poi.phone}, " +
"Photo URLS => ${site.poi.photoUrls}, " +
"Rating => ${site.poi.rating}, " +
"Address Detail => ${site.address.thoroughfare}, ${site.address.subLocality}, " +
"${site.address.locality}, ${site.address.adminArea}, ${site.address.country}")
}
} ?: kotlin.run {
Log.e("SITE_KIT","There is not any site")
}
}

})
}

First, we need to create a SearchService object from the SearchServiceFactory class. For this, we can use the create() method of the SearchServiceFactory class. We need to declare two parameters in create() method.

The first of these parameters is context value. It is recommended that Context value should be in Activity type. Otherwise, when HMS Core(APK) needs to be updated, we can not receive any notification about it.

The second parameter is API Key value that we can access via AppGallery Connect. This value is generated automatically by AppGallery Connect when a new app is created. We need to encode API parameter as encodeURI.

After creating our SearchService object as I described above, we can create a NearbySearchRequest object. We will determine all the criteria on this project which we will perform on search.

To search for nearby places, we first need to create a NearbySearchRequest object. We will specify certain criteria on this object to configure our search results. These criteria provide our search results to modify according to the application logic. Let’s examine these criteria in detail:

Location: Here we need to report user’s location. We can determine a value by ourselves, but it will be more useful in terms of the working logic our application if we declare current location of user or last know location of user in order to obtain more healthy result.

Query: If we want to return places for specific keywords instead of searching for all places, we can use this parameter. It narrows and customizes the search result according to the keywords we have specified.

Radius: It is used to make the search results within in a radius determined in meters. It can take values between 1 and 50000, and its default value is 50000.

PoiType: With PoiType, we can significantly change our search results. It takes an object of type LocationType. With LocationType object, we can specify parameters such as LocationType.AIRPORT, LocationType.MUSEUM, LocationType.PARKING. Thus, our search results can be limited as airport, museum, parking lot etc.

HwPoiType: HwPoiType has joined us with 5.0.0 version of Site Kit. It is used for similar purposes with PoiType. However, HwPoiType contains much more details. In this way, we can have the opportunity to further customize our searches. To use this feature, we need to use values of HwLocationType enum.

To learn more about the difference between LocationType and HwLocationType, let’s examine the differences with an example.

Let’s consider an application that user wants to search for restaurants. If we want to search for restaurants with LocationType object, we need to specify a parameter such as LocationType.RESTAURANT. This will return us all restaurants.

But what should we do when user wants to customize the restaurants? This is where HwLocationType is coming our help right here. With HwLocationType, we can further customize the restaurants we want to search for. For example, if user wants to go to Chinese restaurants, we can specify HwLocationType.CHINESE_RESTAURANT parameter, if user wants to go to Turkish restaurants, we can specify HwLocationType.TURKISH_RESTAURANT parameter.

Note: If we use both PoiType and HwPoiType parameters simultaneously, HwPoiType parameter will be priority.

PageSize: Results return with the Pagination structure. This parameter is used to determine the number of Sites to be found in each page.

PageIndex: It is used to specify the number of the page to be returned with the Pagination structure.

Language: It is used to specify the language that search results have to be returned. If this parameter is not specified, language of the query field we have specified in the query field is accepted by default. In example code snippet in above, language of device has been added automatically in order to get a healthy result.

After determining parameters of search process, we can perform our search process. We need to use nearbySearch() method of SearchService object ot perform the search operation for Nearby Place Search feature. This method takes two parameters.

For the first parameter, we need to specify NearbySearchRequest object we have defined above.

For the second parameter, we have to implement SearchResultListener interface. Since this interface has a generic structure, we need to specify class belonging to the values to be returned. We can get the incoming values by specifying NearbySearchResponse object. Two methods should be override with this interface. onSearchError() method is executed if operation fails, and onSearchResult() method is executed if operation is successful. There are 2 different values in NearbySearchResponse. These are sites and totalCount values. sites, keeps a list of Site object. And, totalCount indicates number of return values.