使用腾讯地图SDK的自动补全功能

说明

本文只使用了腾讯地图的 自动提示 功能

本文只使用了腾讯地图的 自动提示 功能

本文只使用了腾讯地图的 自动提示 功能

其他2D,3D,地图街景等功能请自行查阅官方文档

另外,如果想用高德地图的朋友

请看我的上一篇博客:使用高德地图SDK的自动补全功能

一、集成腾讯地图SDK

1.集成检索SDK有两种方法 - 基于AS,使用Eclipse的童鞋请参考官方文档

使用 maven

矢量地图 v4.0.3.2 开始支持 maven 配置地图sdk,配置方法:

工程顶级 gradle.properties 文件中加入:

maven{
        url "https://oss.sonatype.org/content/groups/public"
}

module build.gradle 文件中添加依赖库名称:

dependencies {
    //这里始终使用最新的稳定版本,用户也可以指定 4.0.3.2 之后的地图 sdk 版本号
    compile 'com.tencent.map:tencent-map-vector-sdk:latest.release'
}

使用JAR包

下载地址:http://lbs.qq.com/android_v1/log_search.html

解压并且把TencentSearch_v1.x.x.jar 加入到libs目录下,右键add as library

参考文档:http://lbs.qq.com/android_v1/guide-project-setup.html

这样就完成了第一步。

2.申请key

申请开发者账号:http://lbs.qq.com/console/user_info.html

需要填写手机和邮箱

进入控制台以后,就可以看到自己的KEY的,在授权应用设置自己的包名

3.在app -> AndroidManifest.xml 加入:

data
      android:name="TencentMapSDK"
      android:value="输入申请的开发者权限"/>

4.混淆

#腾讯地图检索sdk
-keep class com.tencent.lbssearch.**{*;}
-keep class com.google.gson.examples.android.model.** { *; }

完成了工程设置以后,就是撸代码的时间了~

二、开始撸代码

1,创建activity_poi_keyword_search.xml布局文件,如下:


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_keyword"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white"
        android:hint="请输入地址关键字搜索"
        android:paddingLeft="8dp"
        android:textColor="@color/deep_black"
        android:textSize="14sp"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/line_color"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    android.support.v7.widget.RecyclerView>

LinearLayout>

2,创建recyclerview的item布局item_poi_keyword_search.xml


<LinearLayout
    android:id="@+id/ll_item_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    >

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/line_color"/>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="8dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="8dp">

        <TextView
            android:id="@+id/tv_detailAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="@color/deep_black"
            android:textSize="16sp"/>

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="@color/black"
            android:textSize="16sp"/>
    LinearLayout>
LinearLayout>

3,创建实体类TXPoiAddressBean


public class TXPoiAddressBean implements Serializable{
    private String longitude;//经度
    private String latitude;//纬度
    private String title;//信息内容
    private  String adcode;//行政编码
    public  String province;//省
    public  String city;//城市
    private  String district;//区
    private String address; //详细地址

    public TXPoiAddressBean(String lon, String lat, String title, String adcode, String province, String city, String district,String address){
        this.longitude = lon;
        this.latitude = lat;
        this.title = title;
        this.adcode = adcode;
        this.province = province;
        this.city = city;
        this.district = district;
        this.address = address;

    }

    public String getLongitude() {
        return longitude;
    }

    public String getLatitude() {
        return latitude;
    }

    public String getTitle() {
        return title;
    }

    public String getAdcode() {
        return adcode;
    }

    public String getProvince() {
        return province;
    }

    public String getCity() {
        return city;
    }

    public String getDistrict() {
        return district;
    }

    public String getAddress(){ return address; }
}

4,创建适配器TXPoiKeywordSearchAdapter

public class TXPoiKeywordSearchAdapter extends RecyclerView.Adapter<TXPoiKeywordSearchAdapter.MyViewHolder> {

    private List poiAddressBean;
    private Context mContext;
    public TXPoiKeywordSearchAdapter(Context context, List poiAddressBean) {
        this.poiAddressBean  = poiAddressBean;
        this.mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(mContext).inflate(R.layout.item_poi_keyword_search, parent, false);
        return new MyViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        final TXPoiAddressBean poiAddressBean = this.poiAddressBean.get(position);
        holder.tv_detailAddress.setText(poiAddressBean.getTitle());
        holder.tv_content.setText(poiAddressBean.getProvince() + "-" + poiAddressBean.getCity() + "-"
                + poiAddressBean.getDistrict());
        holder.ll_item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((TXPoiKeywordSearchActivity) mContext).setSearchData(poiAddressBean);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (poiAddressBean != null) {
            return poiAddressBean.size();
        } else {
            return 0;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_content;
        TextView     tv_detailAddress;
        LinearLayout ll_item_layout;

        private MyViewHolder(View itemView) {
            super(itemView);
            tv_detailAddress = (TextView) itemView.findViewById(R.id.tv_detailAddress);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            ll_item_layout = (LinearLayout) itemView.findViewById(R.id.ll_item_layout);
        }
    }
}

5,创建搜索界面类TXPoiKeywordSearchActivity


public class TXPoiKeywordSearchActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private EditText mEt_keyword;
    private String keyWord = "";// 搜索关键字

    TencentSearch tencentSearch;
    SuggestionParam suggestionParam;
    private TXPoiAddressBean bean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poi_keyword_search);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        initView();
        initData();
        initListener();
    }


    private void initView() {

        mRecyclerView = (RecyclerView) findViewById(R.id.search_recyclerView);
        mEt_keyword = (EditText) findViewById(R.id.et_keyword);

    }

    private void initData() {

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        tencentSearch = new TencentSearch(this);
    }

    private void initListener() {
        mEt_keyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                keyWord = String.valueOf(charSequence);
                if (!"".equals(keyWord)) {
                    suggestionParam.keyword(keyWord);
                    doSearch();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    // 查询回调
    private void doSearch() {

        tencentSearch.suggestion(suggestionParam, new HttpResponseListener() {
            @Override
            public void onSuccess(int i, BaseObject baseObject) {
                SuggestionResultObject oj = (SuggestionResultObject) baseObject;
                if (oj.data != null) {
                    ArrayList poiList = new ArrayList<>();//自己创建的数据集合

                    for (SuggestionResultObject.SuggestionData data : oj.data) {
                        Location location = data.location;
                        double lng = location.lng;
                        double lat = location.lat;
                        String address = data.address;

                        String title = data.title;
                        String adcode = data.adcode;
                        String provinceName = data.province;
                        String cityName = data.city;
                        String district = data.district;

                        poiList.add(new TXPoiAddressBean(String.valueOf(lng), String.valueOf(lat), title, adcode, provinceName
                                , cityName, district, address));
                    }

                    TXPoiKeywordSearchAdapter adapter = new TXPoiKeywordSearchAdapter(TXPoiKeywordSearchActivity.this, poiList);
                    mRecyclerView.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(int i, String s, Throwable throwable) {
            }
        });
    }


    // 返回搜索结果
    public void setSearchData(TXPoiAddressBean bean) {
        this.bean = bean;
        // 编写业务逻辑
    }

}

6.在AndroidManifest中注册PoiKeywordSearchActivity

7.在需要用到的地方startActivity跳转到PoiKeywordSearchActivity即可


你可能感兴趣的:(Android)