地图位置搜索

来源:互联网 发布:spring mybatis打印sql 编辑:程序博客网 时间:2024/06/06 00:07

Ø  知识点:ArrayList/arrayAdapter、输入提示、搜索

Ø  实现详情

1.      利用控件AutoCompleteTextView,作为适配器,放置输入提示列表,并在其中text内容发生改变时,监听返回输入建议


2.     搜索功能:点击“搜索”按钮,实现搜索功能,监听搜索结果返回,发送请求进行搜索,在点击时要记得清除定位

/** 搜索:poi搜索功能 */    setUpSearch();
private void setUpSearch() {    search = (Button) findViewById(R.id.search);    search.setOnClickListener(this);    editList = (AutoCompleteTextView) findViewById(R.id.searchLocation);    editList.addTextChangedListener(this);    aMap.setOnMarkerClickListener(this);// 添加点击marker监听事件    aMap.setInfoWindowAdapter(this);// 添加显示infowindow监听事件}
/** * 开始进行poi搜索 */private void search() {    keyWord=editList.getText().toString().trim();    if (keyWord != null) {        int currentPage = 0;        query = new PoiSearch.Query(keyWord, "", "");        // keyWord表示搜索字符串,第二个参数表示POI搜索类型,默认为:生活服务、餐饮服务、商务住宅        // cityCode表示POI搜索区域,(这里可以传空字符串,空字符串代表全国在全国范围内进行搜索)        query.setPageSize(10);// 设置每页最多返回多少条poiitem        query.setPageNum(currentPage);// 设置查第一页        poiSearch = new PoiSearch(this, query);        // poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 6000));//设置搜索中心点及范围        poiSearch.setOnPoiSearchListener(this);// 设置数据返回的监听器        poiSearch.searchPOIAsyn();//调用 PoiSearch 的searchPOIAsyn() 方法发送请求,开始搜索    }else{        Toast.makeText(MainActivity.this, "输入为空!", Toast.LENGTH_LONG).show();    }}

@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {    keyWord = s.toString().trim();    if (keyWord != null) {        InputtipsQuery inputquery = new InputtipsQuery(keyWord, "");        Inputtips inputTips = new Inputtips(MainActivity.this, inputquery);        inputTips.setInputtipsListener(this);        inputTips.requestInputtipsAsyn();//调用PoiSearch的requestInputtipsAsyn()方法发送请求    }else {        Toast.makeText(MainActivity.this, "输入为空!", Toast.LENGTH_LONG).show();    }}@Overridepublic void afterTextChanged(Editable s) {}
@Overridepublic 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.input, listString);        editList.setAdapter(aAdapter);        aAdapter.notifyDataSetChanged();    } else {        Log.e("MainActivity", " ErrCode:" + rCode);    }}@Overridepublic void onPoiItemSearched(PoiItem poiItem, int errorCode) {}@Overridepublic void onPoiSearched(PoiResult result, int rCode) {    if (rCode ==1000) {        if (result != null && result.getQuery() != null) {// 搜索poi的结果            if (result.getQuery().equals(query)) {// 是否是同一条                poiResult = result;                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 {                    Toast.makeText(MainActivity.this,"没有可显示的结果",Toast.LENGTH_SHORT).show();                }            }        } else {            Toast.makeText(MainActivity.this,"没有可显示的结果",Toast.LENGTH_SHORT).show();        }    } else if (rCode == 27) {        Toast.makeText(MainActivity.this,"网络错误",Toast.LENGTH_SHORT).show();    } else if (rCode == 32) {        Toast.makeText(MainActivity.this,"错误的Key值",Toast.LENGTH_SHORT).show();    } else {        Toast.makeText(MainActivity.this,"其他的错误",Toast.LENGTH_SHORT).show();    }}/** * 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";    }    Toast.makeText(MainActivity.this,infomation,Toast.LENGTH_SHORT).show();}
@Overridepublic boolean onMarkerClick(Marker marker) {    marker.showInfoWindow();    return false;}
@Overridepublic View getInfoWindow(Marker marker) {    return null;}@Overridepublic View getInfoContents(Marker marker) {    return null;}

1 0
原创粉丝点击