packagebaidumapsdk.demo;
importjava.util.ArrayList;
importjava.util.List;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.DialogInterface;
importandroid.content.DialogInterface.OnClickListener;
importandroid.graphics.Color;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.BaseAdapter;
importandroid.widget.ListView;
importandroid.widget.TextView;
importcom.baidu.mapapi.VersionInfo;
importcom.baidu.mapapi.model.LatLng;
importcom.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
importcom.baidu.mapapi.navi.BaiduMapNavigation;
importcom.baidu.mapapi.navi.NaviParaOption;
importcom.baidu.mapapi.utils.OpenClientUtil;
importcom.baidu.mapapi.utils.poi.BaiduMapPoiSearch;
importcom.baidu.mapapi.utils.poi.PoiParaOption;
importcom.baidu.mapapi.utils.route.BaiduMapRoutePlan;
importcom.baidu.mapapi.utils.route.RouteParaOption;
importcom.baidu.mapapi.utils.route.RouteParaOption.EBusStrategyType;
/**
* 介绍如何调启百度地图实现自身业务功能
* */
publicclassOpenBaiduMapextendsActivity {
// 天安门坐标
doublemLat1 =39.915291;
doublemLon1 =116.403857;
// 百度大厦坐标
doublemLat2 =40.056858;
doublemLon2 =116.308194;
@Override
protectedvoidonCreate(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(newOpenBaiduMapListAdapter(getData()));
mListView.setOnItemClickListener(newOnItemClickListener() {
@Override
publicvoidonItemClick(AdapterView> parent, View view,
intposition,longid) {
switch(position) {
case0:
//启动百度地图导航(native)
startNavi();
break;
case1:
//启动百度地图导航(Web)
startWebNavi();
break;
case2:
//启动百度地图Poi周边检索
startPoiNearbySearch();
break;
case3:
//启动百度地图Poi详情页面
startPoiDetails();
break;
case4:
//启动百度地图步行路线规划
startRoutePlanWalking();
break;
case5:
//启动百度地图驾车路线规划
startRoutePlanDriving();
break;
case6:
//启动百度地图公交路线规划
startRoutePlanTransit();
break;
default:
break;
}
}
});
}
privateList getData(){
List data = newArrayList();
data.add(“启动百度地图导航(Native)”);
data.add(“启动百度地图导航(Web)”);
data.add(“启动百度地图Poi周边检索”);
data.add(“启动百度地图Poi详情页面”);
data.add(“启动百度地图步行路线规划”);
data.add(“启动百度地图驾车路线规划”);
data.add(“启动百度地图公交路线规划”);
returndata;
}
/**
* 启动百度地图导航(Native)
*
*/
publicvoidstartNavi() {
LatLng pt1 = newLatLng(mLat1, mLon1);
LatLng pt2 = newLatLng(mLat2, mLon2);
// 构建导航参数
/**
* endName(java.lang.String endName):导航终点名称
* endPoint(LatLng endPoint):导航终点, 百度经纬度坐标
* startName(java.lang.String startName):导航起点名称
* startPoint(LatLng startPoint):导航起点, 百度经纬度坐标
* */
NaviParaOption para = newNaviParaOption().startPoint(pt1).endPoint(pt2)
.startName(“天安门”).endName(“百度大厦”);
try{
/**
* openBaiduMapNavi(NaviParaOption para, Context context)
* 调起百度地图导航页面
* */
BaiduMapNavigation.openBaiduMapNavi(para, this);
} catch(BaiduMapAppNotSupportNaviException e) {
e.printStackTrace();
showDialog();
}
}
/**
* 启动百度地图导航(Web)
*/
publicvoidstartWebNavi() {
LatLng pt1 = newLatLng(mLat1, mLon1);
LatLng pt2 = newLatLng(mLat2, mLon2);
// 构建 导航参数
NaviParaOption para = newNaviParaOption()
.startPoint(pt1).endPoint(pt2);
/**
* openWebBaiduMapNavi(NaviParaOption para, Context context)
* 调起百度地图导航页面
* */
BaiduMapNavigation.openWebBaiduMapNavi(para, this);
}
/**
* 启动百度地图Poi周边检索
*
*/
publicvoidstartPoiNearbySearch() {
LatLng pt_center = newLatLng(mLat1, mLon1);// 天安门
/**
* poi检索参数
* key(java.lang.String key):poi检索 key
* center(LatLng center):poi检索中心点
* radius(int radius):poi检索半径
* */
PoiParaOption para = newPoiParaOption().key(“天安门”).center(pt_center).radius(2000);
try{
/**
* openBaiduMapPoiNearbySearch(PoiParaOption para, Context context)
* 调起百度地图poi周边检索页面
* */
BaiduMapPoiSearch.openBaiduMapPoiNearbySearch(para, this);
} catch(Exception e) {
e.printStackTrace();
showDialog();
}
}
/**
* 启动百度地图Poi详情页面
*
*/
publicvoidstartPoiDetails() {
/**
* uid(java.lang.String uid)
* poi详情uid
* */
PoiParaOption para = newPoiParaOption().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();
}
}
/**
* 启动百度地图步行路线规划
*
*/
publicvoidstartRoutePlanWalking() {
LatLng pt_start = newLatLng(34.264642646862,108.95108518068);
LatLng pt_end = newLatLng(mLat2, mLon2);
// 构建 route搜索参数
RouteParaOption para = newRouteParaOption()
.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();
}
}
/**
* 启动百度地图驾车路线规划
*
*/
publicvoidstartRoutePlanDriving() {
LatLng pt_start = newLatLng(34.264642646862,108.95108518068);
LatLng pt_end = newLatLng(mLat2, mLon2);
// 构建 route搜索参数
RouteParaOption para = newRouteParaOption()
.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();
}
}
/**
* 启动百度地图公交路线规划
*
*/
publicvoidstartRoutePlanTransit() {
LatLng pt_start = newLatLng(mLat1, mLon1);
LatLng pt_end = newLatLng(mLat2, mLon2);
// 构建 route搜索参数
/**
* busStrategyType(RouteParaOption.EBusStrategyType busStrategyType)
* 路线检索策略枚举(公交必选)
* 时间短 bus_time_first
* 少换乘 bus_transfer_little
* 少步行 bus_walk_little
* 不坐地铁 bus_no_subway
* 推荐路 bus_recommend_way
* */
RouteParaOption para = newRouteParaOption()
// .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
protectedvoidonDestroy() {
super.onDestroy();
BaiduMapRoutePlan.finish(this);
BaiduMapPoiSearch.finish(this);
}
/**
* 提示未安装百度地图app或app版本过低
*
*/
publicvoidshowDialog() {
AlertDialog.Builder builder = newAlertDialog.Builder(this);
builder.setMessage(“您尚未安装百度地图app或app版本过低,点击确认安装?”);
builder.setTitle(“提示”);
builder.setPositiveButton(“确认”,newOnClickListener() {
@Override
publicvoidonClick(DialogInterface dialog,intwhich) {
dialog.dismiss();
/**
* OpenClientUtil:调起百度客户端工具类
*
* public static int getBaiduMapVersion(Context context)
* 获取百度地图客户端版本号
* 返回0代表没有安装百度地图客户端
* */
OpenClientUtil.getLatestBaiduMapApp(OpenBaiduMap.this);
}
});
builder.setNegativeButton(“取消”,newOnClickListener() {
@Override
publicvoidonClick(DialogInterface dialog,intwhich) {
dialog.dismiss();
}
});
builder.create().show();
}
privateclassOpenBaiduMapListAdapterextendsBaseAdapter {
List list;
publicOpenBaiduMapListAdapter(List list) {
super();
this.list = list;
}
@Override
publicView getView(intindex, 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));
returnconvertView;
}
@Override
publicintgetCount() {
returnlist.size();
}
@Override
publicObject getItem(intindex) {
returnlist.get(index);
}
@Override
publiclonggetItemId(intid) {
returnid;
}
}
}