[Huawei Developers] Freemind R:
This article is originally from HUAWEI Developer Forum

Forum link: https://forums.developer.huawei.com/forumPortal/en/home

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403142410.55802869602899707787517559041440:50510527021307:2800:EEC57AA37A620B79B7530BBF3DEC0B5B78EC4F8F4B942B233C7F2894FC3B41BA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403142410.55802869602899707787517559041440:50510527021307:2800:EEC57AA37A620B79B7530BBF3DEC0B5B78EC4F8F4B942B233C7F2894FC3B41BA.png[/IMG]​

HUAWEI Site Kit provides developers with convenient and secure access to diverse, place-related services.

Core Capabilities

Place search: Returns a place list based on keywords entered by the user.

Nearby place search: Searches for nearby places based on the current location of the user's device.

Place details: Searches for details about a place.

Search suggestion: Returns a list of place suggestions.

Today in this article we are going to see how to integrate HMS core site kit into your apps along with that we have used

Ø AutoCompleteTextView

Ø TextWatcher

Ø Html format

Prerequisite

1) Must have a Huawei Developer Account

2) Must have a Huawei phone with HMS 4.0.0.300 or later

3) Must have a laptop or desktop with Android Studio , Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403142703.32444122252982951320105908058206:50510527021307:2800:6DC92A2E136799682F29C07C6839A206DC369856323F399D536612CE28E18E02.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403142703.32444122252982951320105908058206:50510527021307:2800:6DC92A2E136799682F29C07C6839A206DC369856323F399D536612CE28E18E02.png[/IMG]​

Things Need To Be Done

1) First we need to create a project in android studio.

2) Get the SHA Key. For getting the SHA key we can refer to this article.

3) Create an app in the Huawei app gallery connect.

4) Enable site kit setting in Manage APIs section.

5) Provide the SHA Key in App Information Section.

6) Provide storage location.

7) After completing all the above points we need to download the agconnect-services.json from App Information Section. Copy and paste the Json file in the app folder of the android project.

8) Copy and paste the below maven url inside the repositories of buildscript and allprojects ( project build.gradle file )

maven { url 'http://developer.huawei.com/repo/' }

9) Copy and paste the below plugin in the app build.gradle file

apply plugin: 'com.huawei.agconnect'

Also implementation 'com.huawei.hms:site:4.0.1.300' in the dependencies section.

10) Now Sync the gradle.

Let’s begin the chase

There are two ways we can achieve our goal using site kit.

1) Site Kit SDK

2) Site Kit APIs

Pros of Site Kit SDK is that we can use lots of core functionality and we get response it the form of model.

Pros of Site Kit APIs is that we get response in the form of Json object and since we are using APIs, the size of the apk will be less than the apk which is using Site Kit SDK.

Here, we will learn how to use Site Kit SDK in our android project and achieve the benefit.

Main Code

1) First we need to create an object of SearchService and use the SearchServiceFactory class to instantiate the object.

SearchSsearchService = SearchServiceFactory.create(this);

2) We need to create a TextSearchRequest Object, which is used as the request body for search by keyword. We can use many parameter like query, location, radius, bounds, poi types etc. but query parameter is mandatory rest are optional.

TextSearchRequest textSearchRequest = new TextSearchRequest();
textSearchRequest.setQuery(“hotel”);

3) Create SearchResultListener object to listen for the search result

searchService.textSearch(textSearchRequest,
new SearchResultListener<TextSearchResponse>() {
@Override
public void onSearchResult(TextSearchResponse textSearchResponse) {

// Do something with the response …
}

@Override
public void onSearchError(SearchStatus searchStatus) {

}
});

The Result

Main Activity class

public class MainActivity extends AppCompatActivity {

AutoCompleteTextView autoCompleteTextView;
private SearchService searchService;
ArrayList<String> list = new ArrayList<>();
ArrayAdapter<String> adapter;
private ArrayAdapter<String> mAutoCompleteAdapter;
TextView txtHeader, txtDetails;
private Timer timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtHeader = findViewById(R.id.txtHeader);
txtDetails = findViewById(R.id.txtDetails);
txtHeader.setText("HMS SITE MAP");
autoCompleteTextView = findViewById(R.id.autoCompleteTxt);
searchService = SearchServiceFactory.create(this);
mAutoCompleteAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.item);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.addTextChangedListener(new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (timer != null) {
timer.cancel();
}
}

@Override
public void afterTextChanged(final Editable s) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// do your actual work here
if (s.length() > 1) {
list.clear();
final TextSearchRequest textSearchRequest = new TextSearchRequest();
textSearchRequest.setQuery(s.toString());
searchService.textSearch(textSearchRequest, new SearchResultListener<TextSearchResponse>() {
@Override
public void onSearchResult(TextSearchResponse textSearchResponse) {

for (Site site : textSearchResponse.getSites()) {
String values = Html.fromHtml(site.getName() + "<br>"
+ site.getAddress().getCountry() + "<br>"
+ site.getLocation().getLat() + "<br>"
+ site.getLocation().getLng() + "<br>"
+ site.getFormatAddress() + "<br>" +
site.getPoi().getPhone()).toString();

list.add(values);

}
mAutoCompleteAdapter.clear();
mAutoCompleteAdapter.addAll(list);
mAutoCompleteAdapter.notifyDataSetChanged();
}

@Override
public void onSearchError(SearchStatus searchStatus) {

}
});
}
}
}, 200);
}

});
autoCompleteTextView.setAdapter(mAutoCompleteAdapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

showDetails(mAutoCompleteAdapter.getItem(position));

}
});
}

private void showDetails(String item) {
String pattern = Pattern.quote("\\" + "n");
String[] lines = item.split("\\n+");
autoCompleteTextView.setText(lines[0]);
String details = "<font color='red'>PLACE NAME : </font>" + lines[0] + "<br>"
+ "<font color='#CD5C5C'>COUNTRY : </font>" + lines[1] + "<br>"
+ "<font color='#8E44AD'>ADDRESS : </font>" + lines[4] + "<br>"
+ "<font color='#008000'>PHONE : </font>" + lines[5];

txtDetails.setText(Html.fromHtml(details, Html.FROM_HTML_MODE_COMPACT));

}

}

The XML Class

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<include
android:id="@+id/include"
layout="@layout/layout_bar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<AutoCompleteTextView
android:id="@+id/autoCompleteTxt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:inputType="textMultiLine"
android:background="@drawable/border"
android:drawableEnd="@drawable/search"
android:paddingStart="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include"
app:layout_constraintVertical_bias="0.0"/>

<TextView
android:id="@+id/txtDetails"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/autoCompleteTxt"
app:layout_constraintVertical_bias="0.26999998" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="SHOW MAP"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include"
app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

The Result

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403143532.15687419034598785613209500549203:50510527021307:2800:3C63120F40A11AD24F6F69640C3712F42F4A6595E76DBFD52BC49F259D8F297F.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403143532.15687419034598785613209500549203:50510527021307:2800:3C63120F40A11AD24F6F69640C3712F42F4A6595E76DBFD52BC49F259D8F297F.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403143642.69541164785965135328819413494726:50510527021307:2800:52E5CC858D5586A2CD5B56828D80E0860639EF271E5A0FEE3560A754EBD14A78.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403143642.69541164785965135328819413494726:50510527021307:2800:52E5CC858D5586A2CD5B56828D80E0860639EF271E5A0FEE3560A754EBD14A78.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403143914.27037758877315976187464093473117:50510527021307:2800:87AA771CAA9837BD81A4EC60919936E729135D77B50AC870EA6949F6E356BEAA.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200403143914.27037758877315976187464093473117:50510527021307:2800:87AA771CAA9837BD81A4EC60919936E729135D77B50AC870EA6949F6E356BEAA.jpg[/IMG]​

Show Map

In Part 2 of this article, we will get to see how to show a map.