<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/top" android:layout_width="match_parent" android:background="@color/a_top" android:layout_height="50dp"> <LinearLayout android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" android:paddingLeft="10dp" android:paddingRight="10dp" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_centerInParent="true" android:background="@mipmap/back"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:layout_marginLeft="5dp" android:textSize="18dp" android:textColor="@color/white"/> </LinearLayout> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="目的地" android:textSize="18dp" android:layout_centerInParent="true" android:textColor="@color/white"/> </RelativeLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:layout_marginTop="60dp" > <AutoCompleteTextView android:id="@+id/keyWord" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/text_search" android:completionThreshold="1" android:dropDownVerticalOffset="1.0dip" android:hint="请输入关键字" android:textColorHint="@color/darkgrey" android:imeOptions="actionDone" android:inputType="text|textAutoComplete" android:maxLength="20" android:paddingRight="37.0dip" android:singleLine="true" android:textColor="#000000" android:textSize="16.0sp" /> <Button android:id="@+id/searchButton" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="开始搜索" /> </LinearLayout> </RelativeLayout>
import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.amap.api.maps.AMap; import com.amap.api.maps.model.Marker; import com.amap.api.services.core.PoiItem; import com.amap.api.services.core.SuggestionCity; import com.amap.api.services.help.Inputtips; import com.amap.api.services.help.InputtipsQuery; import com.amap.api.services.help.Tip; import com.amap.api.services.poisearch.PoiResult; import com.amap.api.services.poisearch.PoiSearch; import java.util.ArrayList; import java.util.List; import sunease.cn.Adapter.CZ_StarAdapter; import sunease.cn.Bean.MapBean; import sunease.cn.Bean.StarBean; import sunease.cn.R; import sunease.cn.Utils.AMapUtil; import sunease.cn.org.View.ToastUtil; /** * Created by Administrator on 2016/7/16. */ public class CZ_ZHongDian_A extends FragmentActivity implements AMap.OnMarkerClickListener, AMap.InfoWindowAdapter, TextWatcher, PoiSearch.OnPoiSearchListener, View.OnClickListener, Inputtips.InputtipsListener { //private AMap aMap; private AutoCompleteTextView searchText;// 输入搜索关键字 private String keyWord = "";// 要输入的poi搜索关键字 private ProgressDialog progDialog = null;// 搜索时进度条 private PoiResult poiResult; // poi返回的结果 private int currentPage = 0;// 当前页面,从0开始计数 private PoiSearch.Query query;// Poi查询条件类 private PoiSearch poiSearch;// POI搜索 private LinearLayout back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cz_star); init(); } /** * 设置页面监听 */ private void init(){ Button searButton = (Button) findViewById(R.id.searchButton); searButton.setOnClickListener(this); searchText = (AutoCompleteTextView) findViewById(R.id.keyWord); searchText.addTextChangedListener(this);// 添加文本输入框监听事件 back=(LinearLayout)findViewById(R.id.back); back.setOnClickListener(this); } /** * Button点击事件回调方法 */ @Override public void onClick(View v) { switch (v.getId()){ case R.id.back: finish(); break; /** * 点击搜索按钮 */ case R.id.searchButton: searchButton(); // searchButton(); break; default: break; } } /** * 点击搜索按钮 */ public void searchButton() { keyWord = AMapUtil.checkEditText(searchText); if ("".equals(keyWord)) { ToastUtil.show(CZ_ZHongDian_A.this, "请输入搜索关键字"); return; } else { MapBean.CZ_WEIZHISTAR=keyWord; doSearchQuery(); } } /** * 显示进度框 */ private void showProgressDialog() { if (progDialog == null) progDialog = new ProgressDialog(this); progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progDialog.setIndeterminate(false); progDialog.setCancelable(false); progDialog.setMessage("正在搜索:\n" + keyWord); progDialog.show(); } /** * 隐藏进度框 */ private void dissmissProgressDialog() { if (progDialog != null) { progDialog.dismiss(); } } /** * 开始进行poi搜索 */ protected void doSearchQuery() { showProgressDialog();// 显示进度框 currentPage = 0; query = new PoiSearch.Query(keyWord, "", "");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国) query.setPageSize(100);// 设置每页最多返回多少条poiitem query.setPageNum(currentPage);// 设置查第一页 poiSearch = new PoiSearch(this, query); poiSearch.setOnPoiSearchListener(this); poiSearch.searchPOIAsyn(); Intent CZ=new Intent(CZ_ZHongDian_A.this,MainActivity.class); MapBean.MAINNUN=3; startActivity(CZ); } @Override public boolean onMarkerClick(Marker marker) { marker.showInfoWindow(); return false; } @Override public View getInfoContents(Marker marker) { return null; } @Override public View getInfoWindow(final Marker marker) { View view = getLayoutInflater().inflate(R.layout.poikeywordsearch_uri, null); TextView title = (TextView) view.findViewById(R.id.title); title.setText(marker.getTitle()); TextView snippet = (TextView) view.findViewById(R.id.snippet); snippet.setText(marker.getSnippet()); ImageButton button = (ImageButton) view .findViewById(R.id.start_amap_app); // 调起高德地图app button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //startAMapNavi(marker); } }); return view; } /** * poi没有搜索到数据,返回一些推荐城市的信息 */ private void showSuggestCity(List<SuggestionCity> cities) { String infomation = "推荐城市\n"; for (int i = 0; i < cities.size(); i++) { infomation += "城市名称:" + cities.get(i).getCityName() + "城市区号:" + cities.get(i).getCityCode() + "城市编码:" + cities.get(i).getAdCode() + "\n"; } ToastUtil.show(CZ_ZHongDian_A.this, infomation); } @Override public void afterTextChanged(Editable s) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String newText = s.toString().trim(); if (!AMapUtil.IsEmptyOrNullString(newText)) { InputtipsQuery inputquery = new InputtipsQuery(newText,""); Inputtips inputTips = new Inputtips(CZ_ZHongDian_A.this, inputquery); inputTips.setInputtipsListener(this); inputTips.requestInputtipsAsyn(); } } /** * POI信息查询回调方法 */ @Override public void onPoiSearched(PoiResult result, int rCode) { dissmissProgressDialog();// 隐藏对话框 if (rCode == 1000) { if (result != null && result.getQuery() != null) {// 搜索poi的结果 if (result.getQuery().equals(query)) {// 是否是同一条 poiResult = result; // 取得搜索到的poiitems有多少页 List<PoiItem> poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始 List<SuggestionCity> suggestionCities = poiResult .getSearchSuggestionCitys();// 当搜索不到poiitem数据时,会返回含有搜索关键字的城市信息 if (poiItems != null && poiItems.size() > 0) { /*aMap.clear();// 清理之前的图标 PoiOverlay poiOverlay = new PoiOverlay(aMap, poiItems); poiOverlay.removeFromMap(); poiOverlay.addToMap(); poiOverlay.zoomToSpan();*/ } else if (suggestionCities != null && suggestionCities.size() > 0) { showSuggestCity(suggestionCities); } else { ToastUtil.show(CZ_ZHongDian_A.this, R.string.no_result); } } } else { ToastUtil.show(CZ_ZHongDian_A.this, R.string.no_result); } } else { ToastUtil.showerror(this, rCode); } } @Override public void onPoiItemSearched(PoiItem item, int rCode) { // TODO Auto-generated method stub } @Override public void onGetInputtips(List<Tip> tipList, int rCode) { if (rCode == 1000) {// 正确返回 List<String> listString = new ArrayList<String>(); for (int i = 0; i < tipList.size(); i++) { listString.add(tipList.get(i).getName()); } ArrayAdapter<String> aAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.cz_staraddress_item,R.id.online_user_list_item_textview, listString); searchText.setAdapter(aAdapter); aAdapter.notifyDataSetChanged(); } else { ToastUtil.showerror(this, rCode); } } }