[Huawei Developers] Sherlock F:
Nearby Place Search

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20201014095132.80498990394254106344642270875071:50511027012324:2800:6F76BFAEB4232D844E703E0CD7904DB5EF457BD14F4D88C3B42F2852B2070082.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20201014095132.80498990394254106344642270875071:50511027012324:2800:6F76BFAEB4232D844E703E0CD7904DB5EF457BD14F4D88C3B42F2852B2070082.gif[/IMG]

Overview

This Nearby Place Search API can return a list of places around the current location of a user. When the user selects a place, your app obtains the place ID and searches for details about the place.

Before going to this article, first we need to learn how to add custom markers to Map. Check the below link

To customize Maps with Custom Marker and Custom Info Window - Maps Kit

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20201014101948.88394478176450967391160583501207:50511027012324:2800:61CC2F7095346F8B72A15011A26E20EF2BF87FEFC218F4BB11F7D19CF7934F2A.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20201014101948.88394478176450967391160583501207:50511027012324:2800:61CC2F7095346F8B72A15011A26E20EF2BF87FEFC218F4BB11F7D19CF7934F2A.jpg[/IMG]

After clicking any nearby item from that recycler view mapNearByItemClick method will trigger. Here More item getHwLocationType value is set to null.

@SuppressLint("RestrictedApi")
@Override
public void mapNearByItemClick(NearByTypeModel nearByTypeModel) {
if (nearByTypeModel.getHwLocationType() == null) {
if (loutNearByTypeRoot != null) {
loutNearByTypeRoot.removeAllViews();
}
myLocationFab.setVisibility(View.INVISIBLE);
fabMapType.setVisibility(View.INVISIBLE);
setNearByTypeFoodData();
setNearByTypeThingsToDo();
setNearByTypeShopping();
setNearByTypeServices();
bottomSlideToUp(cardViewNearByType);
} else {
if (NetworkUtil.isNetworkConnected(this)) {
if (cardViewNearByType.getVisibility() == View.VISIBLE)
bottomSlideToDown(cardViewNearByType);
isFromNearByPlace = true;
progressBarPlaces.setVisibility(View.VISIBLE);
editTextSearch.setText(nearByTypeModel.getNearByTypeName());
behavior.setPeekHeight(0);
recyclerViewNearByTypes.setVisibility(View.GONE);
nearBySearch(nearByTypeModel.getNearByTypeName(), nearByTypeModel.getHwLocationType(), 20);
} else {
showNetworkDialog();
}
}
}

Create content_near_types.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/dimen_10"
android:orientation="vertical">

<TextView
android:id="@+id/txt_near_by_type_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dimen_10"
android:text="Food and Drinks"
android:textColor="#000"
android:textSize="14sp"
android:textStyle="bold" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_near_by_type"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>

Create root layout for adding content_near_types.xml child

<androidx.cardview.widget.CardView
android:id="@+id/card_near_by_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_50">

<ImageView
android:id="@+id/img_back_near_by_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="@dimen/dimen_10"
android:src="@drawable/back_pc" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:text="More Categories"
android:textColor="#000"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/lout_near_by_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</androidx.cardview.widget.CardView>

Now set data to adapters and add child layouts to root. Check the below code

private void setNearByTypeServices() {
View view = LayoutInflater.from(MapActivity.this).inflate(R.layout.content_near_types, null, false);
TextView txtNearByTypeTitle = view.findViewById(R.id.txt_near_by_type_title);
RecyclerView recyclerViewNearByType = view.findViewById(R.id.recycler_view_near_by_type);
NearByAdapter nearByAdapter = new NearByAdapter(MapActivity.this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(MapActivity.this, 2);
recyclerViewNearByType.setLayoutManager(gridLayoutManager);
recyclerViewNearByType.setAdapter(nearByAdapter);
ArrayList<NearByTypeModel> nearByTypeServices = nearByTypeServices();
txtNearByTypeTitle.setText("Services");
nearByAdapter.addList(nearByTypeServices);
loutNearByTypeRoot.addView(view);
}

private void setNearByTypeShopping() {
View view = LayoutInflater.from(MapActivity.this).inflate(R.layout.content_near_types, null, false);
TextView txtNearByTypeTitle = view.findViewById(R.id.txt_near_by_type_title);
RecyclerView recyclerViewNearByType = view.findViewById(R.id.recycler_view_near_by_type);
NearByAdapter nearByAdapter = new NearByAdapter(MapActivity.this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(MapActivity.this, 2);
recyclerViewNearByType.setLayoutManager(gridLayoutManager);
recyclerViewNearByType.setAdapter(nearByAdapter);
ArrayList<NearByTypeModel> nearByTypeShopping = nearByTypeShopping();
txtNearByTypeTitle.setText("Shopping");
nearByAdapter.addList(nearByTypeShopping);
loutNearByTypeRoot.addView(view);
}

private void setNearByTypeThingsToDo() {
View view = LayoutInflater.from(MapActivity.this).inflate(R.layout.content_near_types, null, false);
TextView txtNearByTypeTitle = view.findViewById(R.id.txt_near_by_type_title);
RecyclerView recyclerViewNearByType = view.findViewById(R.id.recycler_view_near_by_type);
NearByAdapter nearByAdapter = new NearByAdapter(MapActivity.this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(MapActivity.this, 2);
recyclerViewNearByType.setLayoutManager(gridLayoutManager);
recyclerViewNearByType.setAdapter(nearByAdapter);
ArrayList<NearByTypeModel> nearByTypeThingsToDoList = nearByTypeThingsToDo();
txtNearByTypeTitle.setText("Things to do");
nearByAdapter.addList(nearByTypeThingsToDoList);
loutNearByTypeRoot.addView(view);
}

private void setNearByTypeFoodData() {
View view = LayoutInflater.from(MapActivity.this).inflate(R.layout.content_near_types, null, false);
TextView txtNearByTypeTitle = view.findViewById(R.id.txt_near_by_type_title);
RecyclerView recyclerViewNearByType = view.findViewById(R.id.recycler_view_near_by_type);
NearByAdapter nearByAdapter = new NearByAdapter(MapActivity.this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(MapActivity.this, 2);
recyclerViewNearByType.setLayoutManager(gridLayoutManager);
recyclerViewNearByType.setAdapter(nearByAdapter);
ArrayList<NearByTypeModel> foodList = nearByTypeFood();
txtNearByTypeTitle.setText("Food and Drink");
nearByAdapter.addList(foodList);
loutNearByTypeRoot.addView(view);
}

private ArrayList<NearByTypeModel> nearByTypeFood() {
ArrayList<NearByTypeModel> nearByTypeModels = new ArrayList<>();
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Restaurants", HwLocationType.RESTAURANT));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Hotels", HwLocationType.HOTEL));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Bars", HwLocationType.BAR));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Takeaway", HwLocationType.TAKE_AWAY_RESTAURANT));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Coffee", HwLocationType.COFFEE_SHOP));
return nearByTypeModels;
}

private ArrayList<NearByTypeModel> nearByTypeThingsToDo() {
ArrayList<NearByTypeModel> nearByTypeModels = new ArrayList<>();
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Parks", HwLocationType.PARK));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Music Center", HwLocationType.MUSIC_CENTER));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Cinema", HwLocationType.CINEMA));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Art school", HwLocationType.ART_SCHOOL));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Museum", HwLocationType.MUSEUM));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Library", HwLocationType.LIBRARY));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Night life", HwLocationType.NIGHTLIFE));
return nearByTypeModels;
}

private ArrayList<NearByTypeModel> nearByTypeShopping() {
ArrayList<NearByTypeModel> nearByTypeModels = new ArrayList<>();
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Groceries", HwLocationType.GROCERY));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Shopping center", HwLocationType.SHOPPING_CENTER));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Beauty supplies", HwLocationType.BEAUTY_SUPPLY_SHOP));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Electronics store", HwLocationType.ELECTRONICS_STORE));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Car dealers", HwLocationType.CAR_DEALER));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Sports store", HwLocationType.SPORTS_STORE));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Convenience store", HwLocationType.CONVENIENCE_STORE));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Shopping", HwLocationType.SHOPPING));
return nearByTypeModels;
}

private ArrayList<NearByTypeModel> nearByTypeServices() {
ArrayList<NearByTypeModel> nearByTypeModels = new ArrayList<>();
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Dry cleaning", HwLocationType.DRY_CLEANERS));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Electric vehicle charging", HwLocationType.ELECTRIC_VEHICLE_CHARGING_STATION));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Hotels", HwLocationType.HOTEL));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Petrol", HwLocationType.PETROL_STATION));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Hospitals", HwLocationType.GENERAL_HOSPITAL));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Beauty salons", HwLocationType.BEAUTY_SALON));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Post office", HwLocationType.POST_OFFICE));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Parking", HwLocationType.TRUCK_PARKING_AREA));
nearByTypeModels.add(new NearByTypeModel(R.drawable.cinema_holder, "Car wash", HwLocationType.CAR_WASH));
return nearByTypeModels;
}

Find the output in below image

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20201014105142.67730209422484449426767298790881:50511027012324:2800:F3E8E6319B34A8332AE60A682B2FB6DEC612FD6EB1C24BEA8020F38F456E5C12.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/250/698/2640091000002250698.20201014105142.67730209422484449426767298790881:50511027012324:2800:F3E8E6319B34A8332AE60A682B2FB6DEC612FD6EB1C24BEA8020F38F456E5C12.jpg[/IMG]

Now initialize Search Service

private void initSearchService() {
try {
searchService = SearchServiceFactory.create(this, URLEncoder.encode(getString(R.string.api_key), "utf-8"));
} catch (UnsupportedEncodingException e) {
Log.e(TAG + " Exception", "encode apikey error " + e.getMessage());
}
}

Now Create a NearbySearchRequest object, which is used as the request body for nearby place search. Related parameters are as follows, among which location is mandatory and others are optional:

location: current location of a user.

radius: search radius, in meters. The value ranges from 1 to 50000. The default value is 1000. The search radius is used to specify an area where places are searched in priority, but not restrict the search result to this area.

query: search keyword.

poiType: POI type of returned places. The value range is the same as that of LocationType

hwPoiType: Huawei POI type of returned places. This parameter is recommended. The value range is the same as that of HwLocationType.

language: language in which search results are displayed. For details about the value range, refer to language codes in Language Mapping. If this parameter is not passed, English will be used. If English 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

NOTE

If both poiType and hwPoiType are set, search results of hwPoiType take precedence.

The following formula must be met: pageIndex x pageSize ≤ 60

More information, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201380592438280450&fid=0101187876626530001