Android GoogleMap suggestion AutoComplete (二)具体实现

来源:互联网 发布:谷歌拼音输入法linux 编辑:程序博客网 时间:2024/05/20 02:56

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在前一篇Android GoogleMap suggestion AutoComplete (一)申请API KEY中已经讲解过如何申请Key,现在主要来看实现,实现的效果如下:</span>


这个API的URL格式为:

URL =  https://maps.googleapis.com/maps/api/place/autocomplete/json?key=你申请的KEY&input=文本框输入的内容


这个要实现的功能,就是在文本框输入大概的地址内容,通过上面的URl传给google,google会将数据返回,然后我们在将数据以下拉列表的形式显示出来


根据上面的功能,我们的文本框选用的控件自然是AutoCompleteTextView

1.先在布局文件中写好控件,根据自己实际需要去写控件具体属性,具体的关于AutoCompleteTextView的用法在这里不做过多解释

    <AutoCompleteTextView        android:id="@+id/place_content"        style="@style/edit_style"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ellipsize="end"        android:gravity="center_vertical"        android:minEms="5"        android:singleLine="true"        android:textColor="@color/grey"        />

2.在java代码中setContent布局文件,获取控件ID,为控件做好监听,在监听的OnTextChanged的方法中根据输入内容的变化,去调用网络请求发送上面的URL给google,google返回数据为JSON格式,将JSON格式进行解析,得到自己需要的数据,并将数据设置到AutoCompleteTextView的Adapter中,具体见下面代码,注:我的网络请求使用的是volley

解析json数据不难理解,可对照Google返回的数据进行理解

    mCityContent.addTextChangedListener(mTextWatcher);    private TextWatcher mTextWatcher = 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 (start  > 0) {                String strInput = s.toString();                complete(strInput);            }        }        @Override        public void afterTextChanged(Editable s) {                 }    };    private void complete(String input){        String strInput = input.replace(" ", "%20");        String url = https://maps.googleapis.com/maps/api/place/autocomplete/json?key=你申请的KEY&input=文本框输入的内容       JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {            @Override            public void onResponse(JSONObject response) {                try {                    ArrayList<String> resultList;                    JSONArray predictions = response.getJSONArray("predictions");                    resultList = new ArrayList<>(predictions.length());                    for (int i = 0; i < predictions.length(); i++) {                        resultList.add(predictions.getJSONObject(i).getString("description"));                    }                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,                            R.layout.auto_city, R.id.auto_txt, resultList);// 自定义layout<span style="font-family: Arial, Helvetica, sans-serif;">R.layout.auto_city</span>                    mCityContent.setAdapter(adapter);                    adapter.notifyDataSetChanged();                } catch (JSONException e) {                    e.printStackTrace();                }            }            @Override            public void onErrorResponse(VolleyError volleyError) {            }        });    }










0 0