实现效果如图。由于搜索采用的是 高德地图搜索 SDK 所以需要到 高德地图开发者注册账号 获取到 appKey
创建项目,导入 gradle
// 高德地图
compile 'com.amap.api:search:latest.integration'
配置好 AndroidManifest.xml
先写 MainActivity.xml 布局
package zqx.rj.com.gdplacesearch;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.amap.api.services.help.Tip;
/**
* author: HyZhan
* created:2018/9/20 15:06
* desc: TODO
*/
public class MainActivity extends AppCompatActivity {
private TextView mTvPlace;
private static final int REQUEST_PLACE = 1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mBtnSearch = findViewById(R.id.btn_search);
mBtnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, InputTipsActivity.class);
startActivityForResult(intent, REQUEST_PLACE);
}
});
mTvPlace = findViewById(R.id.tv_place);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_PLACE) {
if (resultCode == InputTipsActivity.RESULT_CODE_INPUTTIPS && data != null) {
final Tip tip = data.getParcelableExtra("tip");
if (tip.getName() != null) {
mTvPlace.setText("选择地点是:" + tip.getName());
}
}
}
}
}
初始化相应控件,当点击了 搜索框 按钮跳转到 InputTipsActivity。
onActivityResult 处理当用户点击地点的时候回调(也就是点击 ListView item 回调)
并显示选择地点 显示到 TextView 上,我这里只显示 选择地点,想要获取更多显示信息可以 在 Tip 中获取
然后写 ListView 适配器 InputTipsAdapter 和对应 adapter_inputtips.xml 文件
简单的两个 TextView 控件 第一个控件显示地点,第二个显示详细地址
package zqx.rj.com.gdplacesearch;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.amap.api.services.help.Tip;
import java.util.List;
/**
* 输入提示adapter,展示item名称和地址
*/
public class InputTipsAdapter extends BaseAdapter {
private Context mContext;
private List mListTips;
InputTipsAdapter(Context context, List tipList) {
mContext = context;
mListTips = tipList;
}
@Override
public int getCount() {
if (mListTips != null) {
return mListTips.size();
}
return 0;
}
@Override
public Object getItem(int i) {
if (mListTips != null) {
return mListTips.get(i);
}
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Holder holder;
if (view == null) {
holder = new Holder();
view = LayoutInflater.from(mContext).inflate(R.layout.adapter_inputtips, null);
holder.mName = view.findViewById(R.id.name);
holder.mAddress = view.findViewById(R.id.address);
view.setTag(holder);
} else{
holder = (Holder)view.getTag();
}
if(mListTips == null){
return view;
}
holder.mName.setText(mListTips.get(i).getName());
String address = mListTips.get(i).getAddress();
if(address == null || address.equals("")){
holder.mAddress.setVisibility(View.GONE);
}else{
holder.mAddress.setVisibility(View.VISIBLE);
holder.mAddress.setText(address);
}
return view;
}
class Holder {
TextView mName;
TextView mAddress;
}
}
Tip 这个 bean 存储的是 地点相关信息
这里也是简单初始化控件、复用 ViewHolder 应该大家都看得懂吧~
接下来是 activity_input_tips.xml
这里引用了 SearchView 控件(搜索框),ListView 显示相对于搜索结果。
package zqx.rj.com.gdplacesearch;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import java.util.List;
public class InputTipsActivity extends AppCompatActivity implements SearchView.OnQueryTextListener,
Inputtips.InputtipsListener, OnItemClickListener {
private ListView mInputListView;
private List mCurrentTipList;
private InputTipsAdapter mIntipAdapter;
public static String DEFAULT_CITY = "北京";
public static final int RESULT_CODE_INPUTTIPS = 101;
public static final int REQUEST_SUC = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_input_tips);
super.onCreate(savedInstanceState);
initSearchView();
mInputListView = findViewById(R.id.inputtip_list);
mInputListView.setOnItemClickListener(this);
}
private void initSearchView() {
SearchView searchView = findViewById(R.id.keyWord);
searchView.setOnQueryTextListener(this);
//设置SearchView默认为展开显示
searchView.setIconified(false);
searchView.onActionViewExpanded();
searchView.setIconifiedByDefault(true);
searchView.setSubmitButtonEnabled(false);
}
/**
* 输入提示回调
*/
@Override
public void onGetInputtips(List tipList, int rCode) {
// 正确返回
if (rCode == REQUEST_SUC) {
mCurrentTipList = tipList;
mIntipAdapter = new InputTipsAdapter(getApplicationContext(), mCurrentTipList);
mInputListView.setAdapter(mIntipAdapter);
mIntipAdapter.notifyDataSetChanged();
} else {
Toast.makeText(this, "错误码 :" + rCode, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
if (mCurrentTipList != null) {
Tip tip = (Tip) adapterView.getItemAtPosition(i);
Intent intent = new Intent();
intent.putExtra("tip", tip);
setResult(RESULT_CODE_INPUTTIPS, intent);
this.finish();
}
}
/**
* 按下确认键触发,本例为键盘回车或搜索键
*/
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
/**
* 输入字符变化时触发
*/
@Override
public boolean onQueryTextChange(String newText) {
if (!TextUtils.isEmpty(newText)) {
InputtipsQuery inputquery = new InputtipsQuery(newText, DEFAULT_CITY);
Inputtips inputTips = new Inputtips(InputTipsActivity.this, inputquery);
inputTips.setInputtipsListener(this);
inputTips.requestInputtipsAsyn();
} else {
// 如果输入为空 则清除 listView 数据
if (mIntipAdapter != null && mCurrentTipList != null) {
mCurrentTipList.clear();
mIntipAdapter.notifyDataSetChanged();
}
}
return true;
}
}
onQueryTextChange() 当输入相关字符触发,如果输入不为空的话,创建搜索对象 并 设置监听,异步请求搜索地点。
onGetInputtips() 方法是搜索结果 回调,根据返回状态码判断 1000 表示 搜索成功,其他错误,参考官网错误码对照表
onItemClick() 获取当前点击 position 回调 MainActivity
源码传送门
感谢阅读,下次再见。