博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android学习笔记之百度地图(周边检索poiSearchNearBy跳转页面并输出搜索结果)
阅读量:4104 次
发布时间:2019-05-25

本文共 4364 字,大约阅读时间需要 14 分钟。

package xiaosi.baiduMap;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import com.baidu.mapapi.BMapManager;import com.baidu.mapapi.GeoPoint;import com.baidu.mapapi.MKAddrInfo;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.MKTransitRouteResult;import com.baidu.mapapi.MKWalkingRouteResult;import com.baidu.mapapi.MapActivity;import com.baidu.mapapi.MapController;import com.baidu.mapapi.MapView;import com.baidu.mapapi.PoiOverlay;public class BaiduMapActivity extends MapActivity{	/** Called when the activity is first created. */	private BMapManager mapManager = null;	private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";	private MapView mapView = null;	private MKSearch mKSearch;	private MapController mapController = null;	@Override	public void onCreate(Bundle savedInstanceState)	{		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		mapManager = new BMapManager(getApplication());		mapManager.init(key, null);		super.initMapActivity(mapManager);		mapView = (MapView) findViewById(R.id.mapView);		// 设置启用内置的缩放控件		mapView.setBuiltInZoomControls(true); 		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放		mapController = mapView.getController(); 		// 设置地图zoom级别		mapController.setZoom(12); 				mKSearch = new MKSearch();		mKSearch.init(mapManager, new MySearchListener());// 注意,MKSearchListener只支持一个,以最后一次设置为准		//搜索山东科技大学附近(5000)的KTV		if (mKSearch.poiSearchNearBy("KTV", new GeoPoint(				(int) (36.001618315221194 * 1E6),				(int) (120.11934041976929 * 1E6)), 5000) == -1)		{			System.out.println("失败");		}		else		{			System.out.println("成功");		}	}	public class MySearchListener implements MKSearchListener	{		public void onGetAddrResult(MKAddrInfo arg0, int arg1)		{			/*			 * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息			 */		}		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)		{			/*			 * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回			 */		}		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)		{			String result = "";			/*			 * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回			 */			if (arg0 == null)			{				return;			}			// 清除地图上已有的所有覆盖物			// mapView.getOverlays().clear();			// PoiOverlay是baidu map api提供的用于显示POI的Overlay			PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this,					mapView);			// 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)			poioverlay.setData(arg0.getAllPoi());			// 为地图添加覆盖物			mapView.getOverlays().add(poioverlay);			//刚开始忘记加这几句代码,地图一直没改变,纠结了很长时间			if (arg0.getNumPois() > 0)			{				// 设置其中一个搜索结果所在地理坐标为地图的中心				MKPoiInfo poiInfo = arg0.getPoi(0);				mapController.setCenter(poiInfo.pt);			}						// 遍历当前页返回的搜索结果(默认只返回10个) 			for (MKPoiInfo poiInfo : arg0.getAllPoi())			{				result = result + "\n"+  "名称:" + poiInfo.name + "\n" + "地址:" + poiInfo.address + "\n" + "城市:" + poiInfo.city;			}									//用AlertDialog来显示搜索到的内容			AlertDialog.Builder builder = new AlertDialog.Builder(BaiduMapActivity.this);   	        builder.setTitle("搜索结果");  	        builder.setMessage(result);  	        builder.setPositiveButton("关闭", new android.content.DialogInterface.OnClickListener(){  	  	            public void onClick(DialogInterface dialog, int which) {  	            	dialog.dismiss();	            }  	        });  	        builder.show();					}		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)		{			/*			 * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息			 */		}		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)		{			/*			 * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回			 */		}	}	@Override	protected boolean isRouteDisplayed()	{		return false;	}	@Override	protected void onDestroy()	{		if (mapManager != null)		{			mapManager.destroy();			mapManager = null;		}		super.onDestroy();	}	@Override	protected void onPause()	{		if (mapManager != null)		{			mapManager.stop();		}		super.onPause();	}	@Override	protected void onResume()	{		if (mapManager != null)		{			mapManager.start();		}		super.onResume();	}}

转载地址:http://tadsi.baihongyu.com/

你可能感兴趣的文章
最小二乘法拟合:原理,python源码,C++源码
查看>>
ubuntu 安装mysql
查看>>
c# 计算器
查看>>
C# 简单的矩阵运算
查看>>
gcc 常用选项详解
查看>>
c++输入文件流ifstream用法详解
查看>>
c++输出文件流ofstream用法详解
查看>>
字符编码:ASCII,Unicode 和 UTF-8
查看>>
QT跨MinGW和MSVC两种编译器的解决办法
查看>>
firewalld的基本使用
查看>>
Linux下SVN客户端使用教程
查看>>
i2c-tools
查看>>
Linux分区方案
查看>>
nc 命令详解
查看>>
如何使用 systemd 中的定时器
查看>>
git命令速查表
查看>>
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(五):OpenFeign请求结果处理及重试控制
查看>>