{{announcement.body}}
{{announcement.title}}

Site Kit Part 2 (Nearby Search, Place Details and Place Search Suggestion)

DZone 's Guide to

Site Kit Part 2 (Nearby Search, Place Details and Place Search Suggestion)

In this article find the following concepts: Nearby Place Search, Place Details, and Place Search suggestion.

· Integration Zone ·
Free Resource

Before starting this article follow the previous article Site Kit (Keyword Search) for better understanding and project set up.

In this article find the following concepts.

  • Nearby Place Search
  • Place Details
  • Place Search suggestion

Nearby Place Search: Huawei Site kit feature helps to get nearby places using the current location of the user. For the nearby search, we can set the POI (Point of Interest) where results can be filtered based on POI. Users can search nearby Bakery, School, ATM, etc.

Follow the steps.

Step 1: Create SearchService Object from the SearchServiceFactory

searchService = SearchServiceFactory.create(getActivity(),
API_KEY);

Step 2: Create the NearBySearchRequest object which is used to request the body for nearby search.

NearbySearchRequest request =  new NearbySearchRequest();

Parameters of Nearby Place

Mandatory 

  • locationcurrent location of a user.
Coordinate location =  new Coordinate( 12.9716 77.5946 );

Optional

  • Radius: search radius, in meters. The value ranges from 1 to 50000 meters. The default value is 1000mtr.
  • Query: search keyword.
  • PoiTypes: list of POI types.
  • Language: Language in which search results are returned. For details about the value range, refer to language codes in Language Mapping. If this parameter is not passed, the language of the query field (preferred) or the local language is used.
  • PoliticalView: Political view parameter. The value is a two-digit country code specified in the ISO 3166-1-alpha-2 standard.
  • PageSize: number of records on each page. The value ranges from 1 to 20. The default value is 20.
  • PageIndex: number of the current page. The value ranges from 1 to 60. The default value is 1.

Step 3: Create the SearchResultListener object to get the nearby place results.

Step 4: Use the SearchService Object (refer Step 1) to call the nearbySearch() and pass the nearby service request (refer Step 2) and also pass the SearchResultListener (refer Step 3).

Step 5: Get the nearby search response

Java
 




x
47


 
1
private void findPlaceAroundMe() {
2
  NearbySearchRequest request =  new NearbySearchRequest();
3
  Coordinate location =  new Coordinate( 12.9716 ,  77.5946 );
4
  request.setLocation(location);
5
  request.setQuery( "India" );
6
  request.setRadius( 5000 );
7
  request.setPoiType(LocationType.ADDRESS);
8
  request.setLanguage( "In" );
9
  request.setPageIndex( 1 );
10
  request.setPageSize( 5 );
11
  // Create a search result listener.
12
  SearchResultListener<NearbySearchResponse> resultListener =  new SearchResultListener<NearbySearchResponse>() {
13
  // Return search results upon a successful search.
14
  @Override
15
  public void onSearchResult(NearbySearchResponse results) {
16
  AddressDetail addressDetail;
17
  List<Site> sites = results.getSites();
18
  if (results ==  null || results.getTotalCount() <=  0 || sites ==  null || sites.size() <=  0 ) {
19
  return ;
20
  }
21
  for (Site site : sites) {
22
  Log.i( "TAG" , String.format( "siteId: '%s', name: %s\r\n" , site.getSiteId(), site.getName()));
23
  }
24
  if (results.getSites() !=  null && results.getSites().size() >  0 ) {
25
  for (Site site : results.getSites()) {
26
  searchModel =  new SearchModel();
27
  addressDetail = site.getAddress();
28
  searchModel.setName(site.getName());
29
  searchModel.setFormattedAddress(site.getFormatAddress());
30
  searchModel.setCountry(addressDetail.getCountry());
31
  searchModel.setCountryCode(addressDetail.getCountryCode());
32
  searchModelList.add(searchModel);
33
  }
34
  SearchListAdapter searchListAdapter =  new SearchListAdapter(searchModelList, getActivity());
35
  searchResultList.setAdapter(searchListAdapter);
36
  }
37
  }
38
 
          
39
  // Return the result code and description upon a search exception.
40
  @Override
41
  public void onSearchError(SearchStatus status) {
42
  Log.i( "TAG" ,  "Error : " + status.getErrorCode() +  " " + status.getErrorMessage());
43
  }
44
  };
45
  // Call the nearby place search API.
46
  searchService.nearbySearch(request, resultListener);
47
}



Place Details: Huawei Site kit feature helps to search for details about a place based on the unique ID (Site Id) of the place. SiteId can get from keyword or nearby or Place Suggestion search.

In-Place details we can get the location name, formatted address, location website, location postal code, location phone numbers, and list of location images URL, etc.

Follow the steps.

Step 1: Create SearchService Object from the SearchServiceFactory

searchService = SearchServiceFactory.create(getActivity(),
API_KEY);

Step 2: Create the DetailSearchRequest object which is used to request body for a place details search.

DetailSearchRequest request =  new DetailSearchRequest();

Parameters of Place Details

Mandatory 

  • SiteId: ID of a place.

Optional

  • Language: Language in which search results are returned. For details about the value range, refer to language codes in Language Mapping. If this parameter is not passed, the local language is used.

  • PoliticalViewPolitical view parameter. The value is a two-digit country code specified in the ISO 3166-1-alpha-2 standard.

Step 3: Create the SearchResultListener object to get the place detail results.

Step 4: Use the SearchService Object (refer Step 1) to call the detailSearch () and pass the detail search request (refer Step 2) and also pass the SearchResultListener (refer Step 3).

Step 5: Get a detailed search response.

Java
 




xxxxxxxxxx
1
41


 
1
StringBuilder stringBuilder;
2
private void getDetailResultOfPlaceUsingSiteId(String siteId) {
3
  DetailSearchRequest request =  new DetailSearchRequest();
4
  request.setSiteId(siteId);
5
  request.setLanguage( "En" );
6
  // Create a search result listener.
7
  SearchResultListener<DetailSearchResponse> resultListener =  new SearchResultListener<DetailSearchResponse>() {
8
  // Return search results upon a successful search.
9
  @Override
10
  public void onSearchResult(DetailSearchResponse results) {
11
  Site site = results.getSite();
12
  if (results ==  null || site ==  null ) {
13
  return ;
14
  }
15
  stringBuilder =  new StringBuilder();
16
  stringBuilder.append( "Site Id: " + results.getSite().getSiteId() +  "\n" );
17
  stringBuilder.append( "Name: " + results.getSite().getName() +  "\n" );
18
  stringBuilder.append( "Formatted Address: " + results.getSite().getFormatAddress() +  "\n" );
19
  stringBuilder.append( "Distance: " + results.getSite().getDistance() +  "\n" );
20
  stringBuilder.append( "Country: " + results.getSite().getAddress().getCountry() +  "\n" );
21
  stringBuilder.append( "Country Code: " + results.getSite().getAddress().getCountryCode() +  "\n" );
22
  stringBuilder.append( "Locality: " + results.getSite().getAddress().getLocality() +  "\n" );
23
  stringBuilder.append( "Sub locality: " + results.getSite().getAddress().getSubLocality() +  "\n" );
24
  /* stringBuilder.append("Postal Code: "+results.getSite().getAddress().getPostalCode()+"\n");
25
  stringBuilder.append("Phone: "+results.getSite().getPoi().getPhone()+"\n");
26
  stringBuilder.append("Website: "+results.getSite().getPoi().getWebsiteUrl()+"\n");
27
  stringBuilder.append("Rating: "+results.getSite().getPoi().getRating()+"\n");*/
28
 
          
29
  resultTv.setText(stringBuilder.toString());
30
  Log.i( "TAG" , String.format( "siteId: '%s', name: %s\r\n" , site.getSiteId(), site.getName()));
31
  }
32
 
          
33
  // Return the result code and description upon a search exception.
34
  @Override
35
  public void onSearchError(SearchStatus status) {
36
  Log.i( "TAG" ,  "Error : " + status.getErrorCode() +  " " + status.getErrorMessage());
37
  }
38
  };
39
  // Call the nearby place search API.
40
  searchService.detailSearch(request, resultListener);
41
}



Place Search SuggestionThis Huawei Site kit feature helps us to return search suggestions during the user input.

Follow the steps.

Step 1: Create SearchService Object from the SearchServiceFactory

searchService = SearchServiceFactory.create(getActivity(),
API_KEY);

Step 2: Create the QuerySuggestionRequest object which is used to request the body for a place details search.

QuerySuggestionRequest request =  new QuerySuggestionRequest();

Parameters of Place Search Suggestion

Mandatory 

  • query: search keyword.

Optional

  • Location: longitude and latitude to which search results need to be biased.
  • Radius: search radius, in meters. The value ranges from 1 to 50000meters. The default value is 50000meters.
  • PoiTypes: list of POI types. The options are as follows:
Field Description
GEOCODE

Returns only geocoding results (such as countries, cities, and streets) but not business places (such as hotels, restaurants, and railway stations). This value can be used to eliminate ambiguity in user input.

For example: when the user enters China, China will be returned, but data such as Bank of China will not be returned

ADDRESS A subset of GEOCODE. Returns only geocoding results with accurate addresses, but not business places or coarse-grained geocoding results (such as countries and cities). This type is recommended when the user searches for a complete address.
ESTABLISHMENT

A complementary set of GEOCODE. Returns only business places (such as hotels, restaurants, and railway stations) but not geocoding results (such as countries, cities, and streets).

For example, when a user enters China, data such as the Bank of China will be returned but China will not.

REGIONS

A subset of GEOCODE. Returns only places of the following types:

· LOCALITY

· SUBLOCALITY

· POSTAL_CODE

· COUNTRY

· ADMINISTRATIVE_AREA_LEVEL_1

· ADMINISTRATIVE_AREA_LEVEL_2

CITIES Returns only places of the LOCALITY or ADMINISTRATIVE_AREA_LEVEL_3 type.
  • CountryCode: country code, which complies with the ISO 3166-1 alpha-2 standards. This parameter is used to restrict search results to the specified country.
  • Language: Language in which search results are returned. For details about the value range, refer to language codes in Language Mapping. If this parameter is not passed, the language of the query field (preferred) or the local language is used.
  • PoliticalView: Political view parameter. The value is a two-digit country code specified in the ISO 3166-1-alpha-2 standard.

Step 3: Create the SearchResultListener object to get the place search suggestion results.

Step 4: Use the SearchService Object (refer Step 1) to call the querySuggestion() and pass the query suggestion request (refer Step 2) and also pass the SearchResultListener (refer Step 3).

Step 5: Get the place suggestion search response

Java
 




xxxxxxxxxx
1
42


 
1
private void getQuerySuggestionResult(String query) {
2
  QuerySuggestionRequest request =  new QuerySuggestionRequest();
3
  request.setQuery(query);
4
  Coordinate location =  new Coordinate( 12.9716 ,  77.5946 );
5
  request.setLocation(location);
6
  request.setRadius( 1000 );
7
  request.setCountryCode( "In" );
8
  request.setLanguage( "In" );
9
  // Create a search result listener.
10
  SearchResultListener<QuerySuggestionResponse> resultListener =  new SearchResultListener<QuerySuggestionResponse>() {
11
  // Return search results upon a successful search.
12
  @Override
13
  public void onSearchResult(QuerySuggestionResponse results) {
14
  List<Site> sites = results.getSites();
15
  if (results ==  null || sites ==  null || sites.size() <=  0 ) {
16
  return ;
17
  }
18
  AddressDetail addressDetail;
19
  if (results.getSites() !=  null && results.getSites().size() >  0 ) {
20
  for (Site site : results.getSites()) {
21
  searchModel =  new SearchModel();
22
  addressDetail = site.getAddress();
23
  searchModel.setName(site.getName());
24
  searchModel.setFormattedAddress(site.getFormatAddress());
25
  searchModel.setCountry(addressDetail.getCountry());
26
  searchModel.setCountryCode(addressDetail.getCountryCode());
27
  searchModelList.add(searchModel);
28
  }
29
  SearchListAdapter searchListAdapter =  new SearchListAdapter(searchModelList, getActivity());
30
  searchResultList.setAdapter(searchListAdapter);
31
  }
32
  }
33
 
          
34
  // Return the result code and description upon a search exception.
35
  @Override
36
  public void onSearchError(SearchStatus status) {
37
  Log.i( "TAG" ,  "Error : " + status.getErrorCode() +  " " + status.getErrorMessage());
38
  }
39
  };
40
  // Call the search suggestion API.
41
  searchService.querySuggestion(request, resultListener);
42
}



Conclusion

Huawei Site kit provides the following feature

  • Keyword search
  • Nearby place search
  • Place details
  • Place suggestion search

From the above feature, we can build applications with location and place related applications, we can also show all the places over Huawei map. Also, search the place in the Huawei map using Site Kit.

Topics:
android, application, integration, map, opensource, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}