今天做了一个小例子,就是使用百度地图将搜索到的所有的肯德基店,全部显示在地图上,并换上肯德基图标,当然我使用PoiOverlay来绘制覆盖物(主要是大头针或者说气泡)。虽然是个小例子但是开发并不顺利。主要原因遇到了两个问题:1.使用PoiOverlay不知道在哪里更换大头针。2.MKSearch的init方法中的MKSearchListener无法直接获取某个城市所有的poi点。主要代码如下:
/** * 替换大头针的方法 */ poiOverlay.getItem(i).setMarker(MapDemoActivity.this.getResources().getDrawable(R.drawable.pic_pgreen));//提换大头针
/*获取每一页的poi*/ if(res.getPageIndex() < res.getNumPages() - 1){ /** * 获取指定页的的poi结果. * 一旦此方法执行则MKSearchListener的onGetPoiResult会被重新调用 */ mSearch.goToPoiPage(res.getPageIndex() + 1); } else if(res.getPageIndex() == res.getNumPages() - 1){ // 设置其中一个搜索结果所在地理坐标为地图的中心 mMapView.invalidate(); mMapView.getController().setCenter(res.getPoi(0).pt); }
以下是更换大头针和解决无法直接获取所有poi的完整示例:
BMapApiDemoApp.java主要用于监听网络错误及key的授权情况
package cn.yw.map.demo; import android.app.Application; import android.util.Log; import android.widget.Toast; import com.baidu.mapapi.*; public class BMapApiDemoApp extends Application { static BMapApiDemoApp mDemoApp; //百度MapAPI的管理类 BMapManager mBMapMan = null; // 授权Key // TODO: 请输入您的Key, // 申请地址:http://dev.baidu.com/wiki/static/imap/key/ String mStrKey = "8BB7F0E5C9C77BD6B9B655DB928B74B6E2D838FD";//在使用百度地图的时候需要一个key boolean m_bKeyRight = true; // 授权Key正确,验证通过 // 常用事件监听,用来处理通常的网络错误,授权验证错误等 static class MyGeneralListener implements MKGeneralListener { //返回网络错误 public void onGetNetworkState(int iError) { Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError); Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), "您的网络出错啦!", Toast.LENGTH_LONG).show(); } //返回授权验证错误 public void onGetPermissionState(int iError) { Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError); if (iError == MKEvent.ERROR_PERMISSION_DENIED) { // 授权Key错误: Toast.makeText(BMapApiDemoApp.mDemoApp.getApplicationContext(), "请在BMapApiDemoApp.java文件输入正确的授权Key!", Toast.LENGTH_LONG).show(); BMapApiDemoApp.mDemoApp.m_bKeyRight = false; } } } public void onCreate() { Log.v("BMapApiDemoApp", "onCreate"); mDemoApp = this; mBMapMan = new BMapManager(this); mBMapMan.init(this.mStrKey, new MyGeneralListener()); mBMapMan.getLocationManager().setNotifyInternal(10, 5); // if (mBMapMan != null) { // mBMapMan.destroy(); // mBMapMan = null; // } super.onCreate(); } @Override //建议在您app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗 public void onTerminate() { // TODO Auto-generated method stub if (mBMapMan != null) { mBMapMan.destroy(); mBMapMan = null; } super.onTerminate(); } }
2.MapDemoActivity.java主要用于搜索时poi及更换大头针
package cn.yw.map.demo; import java.util.ArrayList; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.MKAddrInfo; import com.baidu.mapapi.MKBusLineResult; import com.baidu.mapapi.MKDrivingRouteResult; import com.baidu.mapapi.MKPoiInfo; import com.baidu.mapapi.MKPoiResult; import com.baidu.mapapi.MKSearch; import com.baidu.mapapi.MKSearchListener; import com.baidu.mapapi.MKSuggestionResult; import com.baidu.mapapi.MKTransitRouteResult; import com.baidu.mapapi.MKWalkingRouteResult; import com.baidu.mapapi.MapActivity; import com.baidu.mapapi.MapView; import com.baidu.mapapi.PoiOverlay; public class MapDemoActivity extends MapActivity { Button mBtnSearch = null; // 搜索按钮 Button mSuggestionSearch = null; //suggestion搜索 ListView mSuggestionList = null; public static String mStrSuggestions[] = {}; MapView mMapView = null; // 地图View MKSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用 protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.map); BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication(); if (app.mBMapMan == null) { app.mBMapMan = new BMapManager(getApplication()); app.mBMapMan.init(app.mStrKey, new BMapApiDemoApp.MyGeneralListener()); } app.mBMapMan.start();//开启百度地图API // 如果使用地图SDK,请初始化地图Activity super.initMapActivity(app.mBMapMan); mMapView = (MapView)findViewById(R.id.bmapView); mMapView.setBuiltInZoomControls(true); //设置在缩放动画过程中也显示overlay,默认为不绘制 mMapView.setDrawOverlayWhenZooming(true); // MKSearch.setPoiPageCapacity(50); // 初始化搜索模块,注册事件监听 mSearch = new MKSearch(); //该接口返回poi搜索,公交搜索,驾乘路线,步行路线结果 mSearch.init(app.mBMapMan, new MKSearchListener(){ //返回poi搜索结果 public void onGetPoiResult(MKPoiResult res, int type, int error) { // 错误号可参考MKEvent中的定义 if (error != 0 || res == null) { Toast.makeText(MapDemoActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show(); return; } PoiOverlay poiOverlay = new PoiOverlay(MapDemoActivity.this, mMapView);//实例化覆盖物类 poiOverlay.setData(res.getAllPoi());//填充数据 for(int i=0;i<res.getAllPoi().size();i++){ if(i<=5){ /** * 替换大头针的方法 */ poiOverlay.getItem(i).setMarker(MapDemoActivity.this.getResources().getDrawable(R.drawable.pic_pgreen));//提换大头针 } else{ poiOverlay.getItem(i).setMarker(MapDemoActivity.this.getResources().getDrawable(R.drawable.pic_pjia)); } } mMapView.getOverlays().add(poiOverlay); /** * 下面这段代码是用于 * 搜索出搜有Poi的方法 */ /*获取每一页的poi*/ if(res.getPageIndex() < res.getNumPages() - 1){ /** * 获取指定页的的poi结果. * 一旦此方法执行则MKSearchListener的onGetPoiResult会被重新调用 */ mSearch.goToPoiPage(res.getPageIndex() + 1); } else if(res.getPageIndex() == res.getNumPages() - 1){ // 设置其中一个搜索结果所在地理坐标为地图的中心 mMapView.invalidate(); mMapView.getController().setCenter(res.getPoi(0).pt); } } public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error) { } public void onGetTransitRouteResult(MKTransitRouteResult res, int error) { } public void onGetWalkingRouteResult(MKWalkingRouteResult res, int error) { } public void onGetAddrResult(MKAddrInfo res, int error) { } public void onGetBusDetailResult(MKBusLineResult result, int iError) { } // public void onGetSuggestionResult(MKSuggestionResult res, int arg1) { // TODO Auto-generated method stub if (arg1 != 0 || res == null) { Toast.makeText(MapDemoActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show(); return; } int nSize = res.getSuggestionNum(); Log.e("numfd********************************", nSize+""); mStrSuggestions = new String[nSize]; for (int i = 0; i < nSize; i++) { mStrSuggestions[i] = res.getSuggestion(i).city + res.getSuggestion(i).key; } ArrayAdapter<String> suggestionString = new ArrayAdapter<String>(MapDemoActivity.this, android.R.layout.simple_list_item_1,mStrSuggestions); mSuggestionList.setAdapter(suggestionString); Toast.makeText(MapDemoActivity.this, "suggestion callback", Toast.LENGTH_LONG).show(); } }); mSuggestionList = (ListView) findViewById(R.id.listView1); // 设定搜索按钮的响应 mBtnSearch = (Button)findViewById(R.id.search); OnClickListener clickListener = new OnClickListener(){ public void onClick(View v) { SearchButtonProcess(v); } }; mBtnSearch.setOnClickListener(clickListener); // 设定suggestion响应 mSuggestionSearch = (Button)findViewById(R.id.suggestionsearch); OnClickListener clickListener1 = new OnClickListener(){ public void onClick(View v) { SuggestionSearchButtonProcess(v); } }; mSuggestionSearch.setOnClickListener(clickListener1); } void SearchButtonProcess(View v) { if (mBtnSearch.equals(v)) { /*Intent intent = null; intent = new Intent(MapDemoActivity.this, MapViewDemo.class); this.startActivity(intent);*/ EditText editCity = (EditText)findViewById(R.id.city); EditText editSearchKey = (EditText)findViewById(R.id.searchkey); mSearch.poiSearchInCity(editCity.getText().toString(), editSearchKey.getText().toString()); mMapView.invalidate();//刷新视图 } } void SuggestionSearchButtonProcess(View v) { EditText editSearchKey = (EditText)findViewById(R.id.suggestionkey); mSearch.suggestionSearch( editSearchKey.getText().toString()); } @Override protected void onPause() { BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication(); app.mBMapMan.stop(); super.onPause(); } @Override protected void onResume() { BMapApiDemoApp app = (BMapApiDemoApp)this.getApplication(); app.mBMapMan.start(); super.onResume(); } @Override protected boolean isRouteDisplayed() { return false; } }
3.map.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="在" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京" /> <TextView android:text="市内找" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/searchkey" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="天安门" /> <Button android:id="@+id/search" android:text="开始" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="关键词" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/suggestionkey" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="天安门" /> <Button android:id="@+id/suggestionsearch" android:text="关键词搜索" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="74dp" > </ListView> </LinearLayout> <com.baidu.mapapi.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
备注:希望对您有所帮助

326

被折叠的 条评论
为什么被折叠?



