BaiduMap---百度地图官方Demo之调用百度地图(介绍如何调启百度地图实现自身业务功能)




    
    

    

    



BaiduMap---百度地图官方Demo之调用百度地图(介绍如何调启百度地图实现自身业务功能)_第1张图片


package baidumapsdk.demo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.mapapi.VersionInfo;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
import com.baidu.mapapi.navi.BaiduMapNavigation;
import com.baidu.mapapi.navi.NaviParaOption;
import com.baidu.mapapi.utils.OpenClientUtil;
import com.baidu.mapapi.utils.poi.BaiduMapPoiSearch;
import com.baidu.mapapi.utils.poi.PoiParaOption;
import com.baidu.mapapi.utils.route.BaiduMapRoutePlan;
import com.baidu.mapapi.utils.route.RouteParaOption;
import com.baidu.mapapi.utils.route.RouteParaOption.EBusStrategyType;
/**
 * 介绍如何调启百度地图实现自身业务功能
 * */
public class OpenBaiduMap extends Activity {

	// 天安门坐标
	double mLat1 = 39.915291;
	double mLon1 = 116.403857;
	// 百度大厦坐标
	double mLat2 = 40.056858;
	double mLon2 = 116.308194;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_open_baidumap);
		TextView text = (TextView) findViewById(R.id.open_Info);
		text.setTextColor(Color.YELLOW);
		text.setText("当手机没有安装百度地图客户端或版本过低时,默认调起百度地图webApp");
		ListView mListView = (ListView) findViewById(R.id.listView_openBaiduMap);
		mListView.setAdapter(new OpenBaiduMapListAdapter(getData()));
       
        mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					//启动百度地图导航(native)
					startNavi();
					break;

				case 1:
					//启动百度地图导航(Web)
					startWebNavi();
					break;
					
				case 2:
					//启动百度地图Poi周边检索
					startPoiNearbySearch();
					break;
					
				case 3:
					//启动百度地图Poi详情页面
					startPoiDetails();
					break;
					
				case 4:
					//启动百度地图步行路线规划
					startRoutePlanWalking();
					break;
					
				case 5:
					//启动百度地图驾车路线规划
					startRoutePlanDriving();
					break;
					
				case 6:
					//启动百度地图公交路线规划
					startRoutePlanTransit();
					break;
					
				default:
					break;
				}
			}
		});

	}
	
    private List getData(){      
        List data = new ArrayList();
        data.add("启动百度地图导航(Native)");
        data.add("启动百度地图导航(Web)");
        data.add("启动百度地图Poi周边检索");
        data.add("启动百度地图Poi详情页面");
        data.add("启动百度地图步行路线规划");
        data.add("启动百度地图驾车路线规划");
        data.add("启动百度地图公交路线规划");
        return data;
    }
	
	/**
	 * 启动百度地图导航(Native)
	 * 
	 */
	public void startNavi() {
		LatLng pt1 = new LatLng(mLat1, mLon1);
		LatLng pt2 = new LatLng(mLat2, mLon2);
		
		// 构建导航参数
		/**
		 * endName(java.lang.String endName):导航终点名称
		 * endPoint(LatLng endPoint):导航终点, 百度经纬度坐标
		 * startName(java.lang.String startName):导航起点名称
		 * startPoint(LatLng startPoint):导航起点, 百度经纬度坐标
		 * */
		NaviParaOption para = new NaviParaOption().startPoint(pt1).endPoint(pt2)
								.startName("天安门").endName("百度大厦");

		try {
			/**
			 * openBaiduMapNavi(NaviParaOption para, Context context)
			 * 调起百度地图导航页面
			 * */
			BaiduMapNavigation.openBaiduMapNavi(para, this);
		} catch (BaiduMapAppNotSupportNaviException e) {
			e.printStackTrace();
			showDialog();
		}
		
	}

	/**
	 * 启动百度地图导航(Web)
	 */
	public void startWebNavi() {
		LatLng pt1 = new LatLng(mLat1, mLon1);
		LatLng pt2 = new LatLng(mLat2, mLon2);
		// 构建 导航参数
		NaviParaOption para = new NaviParaOption()
				.startPoint(pt1).endPoint(pt2);
		/**
		 * openWebBaiduMapNavi(NaviParaOption para, Context context)
		 * 调起百度地图导航页面
		 * */
		BaiduMapNavigation.openWebBaiduMapNavi(para, this);
	}
	
	/**
	 * 启动百度地图Poi周边检索
	 * 
	 */
	public void startPoiNearbySearch() {
		LatLng pt_center = new LatLng(mLat1, mLon1); // 天安门
		/**
		 * poi检索参数
		 *  	key(java.lang.String key):poi检索 key
		 *      center(LatLng center):poi检索中心点
		 *      radius(int radius):poi检索半径
		 * */
		PoiParaOption para = new PoiParaOption().key("天安门").center(pt_center).radius(2000);
		
		try {
			/**
			 * openBaiduMapPoiNearbySearch(PoiParaOption para, Context context)
			 * 调起百度地图poi周边检索页面
			 * */
			BaiduMapPoiSearch.openBaiduMapPoiNearbySearch(para, this);
		} catch (Exception e) {
			e.printStackTrace();
			showDialog();
		}
	
	}
	
	/**
	 * 启动百度地图Poi详情页面
	 * 
	 */
	public void startPoiDetails() {
		/**
		 * uid(java.lang.String uid)
		 * poi详情uid
		 * */
		PoiParaOption para = new PoiParaOption().uid("65e1ee886c885190f60e77ff");// 天安门
		try {
			/**
			 * public static void openBaiduMapPoiDetialsPage(PoiParaOption para,Context context)
			 * 调起百度地图poi详情页面
			 * @param     
			 * para - poi检索参数,uid不能为空。
			 * context - 启动poi详情页面的Activity.
			 * 
			 * */
			BaiduMapPoiSearch.openBaiduMapPoiDetialsPage(para, this);
		} catch (Exception e) {
			e.printStackTrace();
			showDialog();
		}

	}
	
	/**
	 * 启动百度地图步行路线规划
	 * 
	 */
	public void startRoutePlanWalking() {
		LatLng pt_start = new LatLng(34.264642646862, 108.95108518068);
		LatLng pt_end = new LatLng(mLat2, mLon2);
		
		// 构建 route搜索参数
		RouteParaOption para = new RouteParaOption()
			.startPoint(pt_start)//路线检索起点
//			.startName("天安门")
//			.endPoint(pt_end);
			.endName("大雁塔")//路线检索终点名称
			.cityName("西安");//城市名称
		
//		RouteParaOption para = new RouteParaOption()
//		.startName("天安门").endName("百度大厦");
		
//		RouteParaOption para = new RouteParaOption()
//		.startPoint(pt_start).endPoint(pt_end);
		
		try {
			/**
			 * openBaiduMapWalkingRout(RouteParaOption para, Context context):
			 * 调起百度地图步行路线检索页面
			 * */
			BaiduMapRoutePlan.openBaiduMapWalkingRout(para, this);
		} catch (Exception e) {
			e.printStackTrace();
			showDialog();
		}
			
	}
	
	/**
	 * 启动百度地图驾车路线规划
	 * 
	 */
	public void startRoutePlanDriving() {
		LatLng pt_start = new LatLng(34.264642646862, 108.95108518068);
		LatLng pt_end = new LatLng(mLat2, mLon2);
		
		// 构建 route搜索参数
		RouteParaOption para = new RouteParaOption()
			.startPoint(pt_start)
//			.startName("天安门")
//			.endPoint(pt_end);
			.endName("大雁塔")
			.cityName("西安");
		
//		RouteParaOption para = new RouteParaOption()
//				.startName("天安门").endName("百度大厦");
					
//		RouteParaOption para = new RouteParaOption()
//		.startPoint(pt_start).endPoint(pt_end);
		
		try {
			/**
			 *openBaiduMapDrivingRoute(RouteParaOption para, Context context)
			 *调起百度地图驾车路线检索页面
			 * */
			BaiduMapRoutePlan.openBaiduMapDrivingRoute(para, this);	
		} catch (Exception e) {
			e.printStackTrace();
			showDialog();
		}
		
	}

	/**
	 * 启动百度地图公交路线规划
	 * 
	 */
	public void startRoutePlanTransit() {
		LatLng pt_start = new LatLng(mLat1, mLon1);
		LatLng pt_end = new LatLng(mLat2, mLon2);
		
		// 构建 route搜索参数
		/**
		 * busStrategyType(RouteParaOption.EBusStrategyType busStrategyType)
		 * 路线检索策略枚举(公交必选) 
		 * 时间短 bus_time_first 
		 * 少换乘 bus_transfer_little 
		 * 少步行 bus_walk_little 
		 * 不坐地铁 bus_no_subway 
		 * 推荐路 bus_recommend_way
		 * */
		RouteParaOption para = new RouteParaOption()
//			.startPoint(pt_start)
			.startName("天安门")
			.endPoint(pt_end)
//			.endName("百度大厦")
			.busStrategyType(EBusStrategyType.bus_recommend_way);

//		RouteParaOption para = new RouteParaOption()
//				.startName("天安门").endName("百度大厦").busStrategyType(EBusStrategyType.bus_recommend_way);
		
//		RouteParaOption para = new RouteParaOption()
//		.startPoint(pt_start).endPoint(pt_end).busStrategyType(EBusStrategyType.bus_recommend_way);
		
		try {
			/**
			 *openBaiduMapTransitRoute(RouteParaOption para, Context context)
			 *调起百度地图公交路线检索页面
			 * */
			BaiduMapRoutePlan.openBaiduMapTransitRoute(para, this);
		} catch (Exception e) {
			e.printStackTrace();
			showDialog();
		}
		
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		BaiduMapRoutePlan.finish(this);
		BaiduMapPoiSearch.finish(this);
	}
	/**
	 * 提示未安装百度地图app或app版本过低
	 * 
	 */
	public void showDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("您尚未安装百度地图app或app版本过低,点击确认安装?");
		builder.setTitle("提示");
		builder.setPositiveButton("确认", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				/**
				 * OpenClientUtil:调起百度客户端工具类
				 * 
				 * public static int getBaiduMapVersion(Context context)
				 * 获取百度地图客户端版本号
				 * 返回0代表没有安装百度地图客户端
				 * */
				OpenClientUtil.getLatestBaiduMapApp(OpenBaiduMap.this);
			}
		});

		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		builder.create().show();
		
	}
	
	
	private class OpenBaiduMapListAdapter extends BaseAdapter {
		List list;
		public OpenBaiduMapListAdapter(List list) {
			super();
			this.list = list;
		}

		@Override
		public View getView(int index, View convertView, ViewGroup parent) {
			convertView = View.inflate(OpenBaiduMap.this,
					R.layout.demo_info_item, null);
			TextView title = (TextView) convertView.findViewById(R.id.title);
			TextView desc = (TextView) convertView.findViewById(R.id.desc);
			desc.setVisibility(View.INVISIBLE);
			title.setText(list.get(index));

			return convertView;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int index) {
			return list.get(index);
		}

		@Override
		public long getItemId(int id) {
			return id;
		}
	}
}


你可能感兴趣的:(Android百度地图)