地图位置搜索
2016-10-10 11:29
204 查看
Ø 知识点:ArrayList/arrayAdapter、输入提示、搜索
Ø 实现详情:
1. 利用控件AutoCompleteTextView,作为适配器,放置输入提示列表,并在其中text内容发生改变时,监听返回输入建议
2. 搜索功能:点击“搜索”按钮,实现搜索功能,监听搜索结果返回,发送请求进行搜索,在点击时要记得清除定位
Ø 实现详情:
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(); } }
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public 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(); } } @Override public void afterTextChanged(Editable s) { }
@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.input, listString); editList.setAdapter(aAdapter); aAdapter.notifyDataSetChanged(); } else { Log.e("MainActivity", " ErrCode:" + rCode); } } @Override public void onPoiItemSearched(PoiItem poiItem, int errorCode) { } @Override public 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(); }
@Override public boolean onMarkerClick(Marker marker) { marker.showInfoWindow(); return false; }
@Override public View getInfoWindow(Marker marker) { return null; } @Override public View getInfoContents(Marker marker) { return null; }
相关文章推荐
- 腾讯地图可搜索定点位置
- Android 百度地图(二)——地图切换、定位、添加覆盖物、搜索位置、连线
- android,使用高德地图SDk(地图SDK,定位SDK,搜索SDK),显示当前位置以及自定义多个Markers
- 地图位置搜索
- 微信公众号开发《三》微信JS-SDK之地理位置的获取,集成百度地图实现在线地图搜索
- JavaScript地图手动输入起始与终点位置搜索
- js调用百度地图API创建地图,搜索位置
- 微信公众号开发《三》微信JS-SDK之地理位置的获取与在线导航,集成百度地图实现在线地图搜索
- 高德地图返回当前定位位置功能
- 百度地图搜索位置
- 地图搜索
- 改进地图搜索用户体验,还是完善
- 微信小程序 实现获取当前位置并在地图上显示
- 地图上查找指定位置
- 位置与地图(二)地图的使用以及标注地图
- c# JD快速搜索工具,2015分析JD搜索报文,模拟请求搜索数据,快速定位宝贝排行位置。
- iOS 地图位置处理
- android-4集成高德地图的搜索和导航功能
- [ZOJ 1002] Fire Net (简单地图搜索)
- iOS应用开发之地图位置解析