实现效果如图 下载代码需注意修改AndroidManifest.xml文件下的百度地图apikey和包名om.baidu.lbsapi.API_KEY"android:value="TaluyMYPi3bpZaVrMDeQZsdV"
工程下载:http://download.csdn.net/detail/b275518834/7861097
<pre name="code" class="java">package app.apad.lp.czxt; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import baidumapsdk.demo.DemoApplication; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.map.ItemizedOverlay; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.OverlayItem; import com.baidu.mapapi.search.MKAddrInfo; import com.baidu.mapapi.search.MKBusLineResult; import com.baidu.mapapi.search.MKDrivingRouteResult; import com.baidu.mapapi.search.MKPlanNode; import com.baidu.mapapi.search.MKPoiInfo; import com.baidu.mapapi.search.MKPoiResult; import com.baidu.mapapi.search.MKSearch; import com.baidu.mapapi.search.MKSearchListener; import com.baidu.mapapi.search.MKShareUrlResult; import com.baidu.mapapi.search.MKSuggestionResult; import com.baidu.mapapi.search.MKTransitRouteResult; import com.baidu.mapapi.search.MKWalkingRouteResult; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Filter; import android.widget.MultiAutoCompleteTextView; import android.widget.Toast; public class BaseMapDaohang_new extends Activity { private AutoCompleteTextView edittext_search; MKPlanNode stNode; MKSearch mSearch; private MapView mMapView; private View btn_search; ArrayList<MKPoiInfo> list_MKPoiInfo = new ArrayList<MKPoiInfo>(); String[] arr_MKPoiInfo = new String[] {}; private ArrayAdapter<String> adapter; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); DemoApplication app = (DemoApplication) this.getApplication(); if (app.mBMapManager == null) { app.mBMapManager = new BMapManager(getApplicationContext()); /** * 如果BMapManager没有初始化则初始化BMapManager */ app.mBMapManager.init(new DemoApplication.MyGeneralListener()); } setContentView(R.layout.baidu_daohang_new); mMapView = (MapView) findViewById(R.id.bmapView); edittext_search = (AutoCompleteTextView) findViewById(R.id.edittext_search); edittext_search.addTextChangedListener(new TextWatcher() { private ArrayAdapter<String> adapter; public void afterTextChanged(Editable editable) { if (adapter != null) { adapter.notifyDataSetChanged(); adapter.notifyDataSetInvalidated(); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { searchButtonProcess(edittext_search); } }); edittext_search.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) { // TODO Auto-generated method stub Toast.makeText(BaseMapDaohang_new.this, "" + index, 1).show(); } }); mSearch = new MKSearch(); stNode = new MKPlanNode(); stNode.name = edittext_search.getText().toString(); mSearch.init(app.mBMapManager, new MKSearchListener() { @Override public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1, int arg2) { // TODO Auto-generated method stub } /** * 在此处理poi搜索结果 */ public void onGetPoiResult(MKPoiResult res, int type, int error) { try { // 错误号可参考MKEvent中的定义 if (error != 0 || res == null) { // Toast.makeText(BaseMapDaohang_new.this, "抱歉,未找到结果", // Toast.LENGTH_LONG).show(); list_MKPoiInfo.clear(); show(); return; } // 将地图移动到第一个POI中心点 if (res.getCurrentNumPois() > 0) { // 将poi结果显示到地图上 MyPoiOverlay poiOverlay = new MyPoiOverlay( BaseMapDaohang_new.this, mMapView, mSearch); poiOverlay.setData(res.getAllPoi()); System.err.println(res.getAllPoi()); list_MKPoiInfo.clear(); for (MKPoiInfo info : res.getAllPoi()) { System.err.println(info.address + " " + info.pt); list_MKPoiInfo.add(info); } mMapView.getOverlays().clear(); mMapView.getOverlays().add(poiOverlay); mMapView.refresh(); // 当ePoiType为2(公交线路)或4(地铁线路)时, poi坐标为空 for (MKPoiInfo info : res.getAllPoi()) { if (info.pt != null) { mMapView.getController().animateTo(info.pt); System.err.println(info.address + " " + info.pt); break; } } show(); } else if (res.getCityListNum() > 0) { // 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表 String strInfo = "在"; for (int i = 0; i < res.getCityListNum(); i++) { strInfo += res.getCityListInfo(i).city; strInfo += ","; } strInfo += "找到结果"; System.err.println("res" + strInfo); Toast.makeText(BaseMapDaohang_new.this, strInfo, Toast.LENGTH_LONG).show(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onGetPoiDetailSearchResult(int arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) { // TODO Auto-generated method stub } @Override public void onGetAddrResult(MKAddrInfo res, int error) { } }); } protected void show() { // TODO Auto-generated method stub if (list_MKPoiInfo == null || list_MKPoiInfo.isEmpty()) { arr_MKPoiInfo = new String[] {}; } else { final int size = list_MKPoiInfo.size(); arr_MKPoiInfo = new String[size]; for (int i = 0; i < list_MKPoiInfo.size(); i++) { arr_MKPoiInfo[i] = list_MKPoiInfo.get(i).address; Log.d("test", arr_MKPoiInfo[i]); } } Log.d("test", list_MKPoiInfo.toString()); adapter = new ArrayAdapter<String>(BaseMapDaohang_new.this, android.R.layout.simple_dropdown_item_1line, arr_MKPoiInfo) { private Filter f; @Override public Filter getFilter() { // TODO Auto-generated method stub if (f == null) { f = new Filter() { @Override protected synchronized FilterResults performFiltering( CharSequence c) { // TODO Auto-generated method stub ArrayList<Object> suggestions = new ArrayList<Object>(); for (String adr : arr_MKPoiInfo) { suggestions.add(adr); } Log.d("test", list_MKPoiInfo.toString()); Log.d("test", arr_MKPoiInfo.toString()); FilterResults filterResults = new FilterResults(); filterResults.values = suggestions; filterResults.count = suggestions.size(); return filterResults; } @Override protected synchronized void publishResults( CharSequence c, FilterResults results) { if (results.count > 0) { adapter.notifyDataSetChanged(); } else { adapter.notifyDataSetInvalidated(); } } }; } return f; } }; edittext_search.setAdapter(adapter); adapter.notifyDataSetChanged(); adapter.notifyDataSetInvalidated(); } /** * 影响搜索按钮点击事件 * * @param v */ public void searchButtonProcess(View v) { mSearch.poiSearchInCity("北京", edittext_search.getText().toString()); } }