一.准备工作:

百度地图移动版API(Android)是一套基于Android设备的应用程序接口,通过该接口,可以轻松访问百度服务和数据,构建功能丰富、交互性强的地图应用程序  百度地图移动版API不仅包含构建地图的基本接口,还提供了本地搜索、路线规划、地图定位等数据服务。

一.获取API Key
地址:百度地图开发官网http://developer.baidu.com/map/android-mobile-apply-key.htm
添加引擎到Andoid工程中
添加jar包: baidumapapi_v3_3_0.jar和locSDK_5.0.jar
添加.so文件:拷贝libBaiduMapSDK_v3_3_0_15.so 、 liblocSDK5.so到libs\armeabi目录下
注: locSDK_5.0.jar和liblocSDK5.so为百度定位SDK所使用资源,开发者可根据实际需求自行添加。

1、打开命令行输入cd .android 进入到android签名目录 如:C:\Users\lenovo\.android
2、命令 -list -v -keystore debug.keystore 密码是android
数字签名;包名 分号是英文的  
例如:48:8C:D9:43:56:81:C4:10:10:40:B6:C3:0A:A1:84:A9:65:65:9A:5A;baidumapsdk.demo
生成的key:V3ITMGba32313GGCLFLALQdP

二.AndroidManifest中的配置

<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!-- 用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!-- 访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<!--允许应用读取低级别的系统日志文件 -->
<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>

key:

<meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="5RcUpEpQyGNZWeTQVGb7gStr" />

三.MainActivity  页面的配置和地图的校验

public class MainActivity extends Activity {private static String[] objects = new String[] { "hello world", "图层","圆形覆盖物", "展示文字", "marker覆盖物", "矩形范围内搜索", "圆形区域", "全城搜索", "驾车路线","步行路线", "公交换乘", "我的位置" };
private static Class[] clazzs = new Class[] { HelloWorld.class,LayerDemo.class,CircleOptionsDemo.class,TextOptionsDemo.class,MarkerOptionsDemo.class,PoiSearchInBoundsDemo.class,PoiSearchNearbyDemo.class,PoiSearchCityDemo.class,DrivingRouteOverlayDemo.class,WalkingRouteOverlayDemo.class,TransitRouteOverlayDemo.class,LocationDemo.class};
private ListView list;
private ArrayAdapter<String> adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);SDKInitializer.initialize(getApplicationContext());//初始化地图list = (ListView) findViewById(R.id.list);adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item, objects);list.setAdapter(adapter);list.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Intent intent = new Intent(getApplicationContext(),clazzs[position]);startActivity(intent);}});}
}<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="${relativePackage}.${activityClass}" ><ListViewandroid:id="@+id/list"android:layout_width="fill_parent"android:layout_height="wrap_content" ></ListView></RelativeLayout>

三.检验百度地图 (测试)

//百度地图,测试
public class HelloWorld extends Activity {private static final String TAG = "tag";private MybaiduMapReceiver baduSdkReceiver;private IntentFilter filter;private MapView mapView;private double latitude = 30.472473;// 纬度private double longitude = 114.418332;// 经度private LatLng hmPos = new LatLng(latitude, longitude);// 学校private BaiduMap map;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 初始化地图,因为百度会校验你的key是不是他的initManager();setContentView(R.layout.common);//初始化设置init();}private void init() {// 设置地图的放大缩小的级别(V2.X 3-19 V1.X 3-18)// ① 修改了文件的格式 优化了空间的使用(北京 110M 15M)// ② 增加了级别 3D效果(18 19)mapView = (MapView)findViewById(R.id.mapView);//mapView.showZoomControls(false);// 默认是true 显示标尺//mapView.showScaleControl(false);// 默认是true 显示缩放按钮// BaiduMap: 管理具体的某一个MapView : 旋转,移动,缩放,事件。。map = mapView.getMap();//默认级别为12,设置19MapStatusUpdate mapStatus = MapStatusUpdateFactory.zoomTo(19);map.setMapStatus(mapStatus);//设置初始化的位置MapStatusUpdate mapStatusPoint = MapStatusUpdateFactory.newLatLng(hmPos);map.setMapStatus(mapStatusPoint );}private void initManager() {SDKInitializer.initialize(getApplicationContext());baduSdkReceiver = new MybaiduMapReceiver();filter = new IntentFilter();filter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);// 注册网络错误filter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR); // 注册key校验结果registerReceiver(baduSdkReceiver, filter);}class MybaiduMapReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String result = intent.getAction();if (result.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) {// 网络错误Toast.makeText(getApplicationContext(), "无网络", 0).show();} else if (result.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) {// key校验失败Toast.makeText(getApplicationContext(), "校验失败", 0).show();}}}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubswitch (keyCode) {case KeyEvent.KEYCODE_1:// 放大// 放大地图缩放级别 每次放大一个级别MapStatusUpdate zoomInStatus = MapStatusUpdateFactory.zoomIn();map.setMapStatus(zoomInStatus);break;case KeyEvent.KEYCODE_2:// 缩小// 缩小地图缩放级别 每次缩小一个级别MapStatusUpdate zooOutStatus = MapStatusUpdateFactory.zoomOut();map.setMapStatus(zooOutStatus);break;case KeyEvent.KEYCODE_3:// 以一个点为中心 旋转MapStatus mapStatus = map.getMapStatus();// 获取地图的当前状态float rotate = mapStatus.rotate;Log.d(TAG, "rotate:" + rotate);// 旋转 范围0~360MapStatus rotateStatus = new  MapStatus.Builder().rotate(rotate+30).build();// 创建新的MapStatusMapStatusUpdate rotateStatusUpdate = MapStatusUpdateFactory.newMapStatus(rotateStatus);map.setMapStatus(rotateStatusUpdate);break;case KeyEvent.KEYCODE_4:// 已一条直线 为轴 旋转 Overlooking 俯角MapStatus mapStatusOver = map.getMapStatus();// 获取地图的当前状态float overlook = mapStatusOver.overlook;Log.d(TAG, "overlook:" + overlook);// 0~45MapStatus overStatus = new MapStatus.Builder().overlook(overlook-5).build();MapStatusUpdate overStatusUpdate = MapStatusUpdateFactory.newMapStatus(overStatus);map.setMapStatus(overStatusUpdate);break;case KeyEvent.KEYCODE_5:// 移动MapStatusUpdate moveStatusUpdate = MapStatusUpdateFactory.newLatLng(new LatLng(40.065796,116.349868));// 带动画的更新地图状态 耗时300毫秒map.animateMapStatus(moveStatusUpdate);break;default:break;}return super.onKeyDown(keyCode, event);}@Overrideprotected void onResume() {mapView.onResume();super.onResume();}@Overrideprotected void onPause() {mapView.onPause();super.onPause();}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(baduSdkReceiver);}
}布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><com.baidu.mapapi.map.MapView//百度地图的Viewandroid:id="@+id/mapView"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
</pre><h2>四.图层示例(普通图层.交通图层.卫星图层)</h2><div><pre name="code" class="java">public class LayerDemo extends Activity {private MapView mapView;private BaiduMap baiduMap;private double latitude = 30.472473;// 纬度private double longitude = 114.418332;// 经度private LatLng hmPos = new LatLng(latitude, longitude);// 学校@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);// 初始化地图,因为百度会校验你的key是不是他的initManager();setContentView(R.layout.common);init();}private void initManager() {SDKInitializer.initialize(getApplicationContext());}private void init() {mapView = (MapView) findViewById(R.id.mapView);baiduMap = mapView.getMap();// 默认级别为12,设置19MapStatusUpdate mapStatus = MapStatusUpdateFactory.zoomTo(12);baiduMap.setMapStatus(mapStatus);// 设置初始化的位置MapStatusUpdate mapStatusPoint = MapStatusUpdateFactory.newLatLng(hmPos);baiduMap.setMapStatus(mapStatusPoint);//baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);卫星图baiduMap.setTrafficEnabled(true);//设置交通图}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_1:// 底图// 设置地图类型 MAP_TYPE_NORMAL 普通图; MAP_TYPE_SATELLITE 卫星图baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);baiduMap.setTrafficEnabled(false);break;case KeyEvent.KEYCODE_2:// 卫星图baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);baiduMap.setTrafficEnabled(false);break;case KeyEvent.KEYCODE_3:// 交通图// 是否打开交通图层baiduMap.setTrafficEnabled(true);break;default:break;}return super.onKeyDown(keyCode, event);}}

五.BaseActivity (代码提取)

public class BaseActivity extends Activity {protected BaiduMap baiduMap;protected MapView mapview;public double latitude = 30.472473;// 纬度public double longitude = 114.418332;// 经度public LatLng hmPos = new LatLng(latitude, longitude);// 学校@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initManager();setContentView(R.layout.common);init();}private void initManager() {//SDKInitializer.initialize(getApplicationContext()); // 不能传递Activity,必须是全局Context}private void init() {// 设置地图级别(V2.X 3-19 V1.X 3-18)// ① 修改了文件的格式 优化了空间的使用(北京 110M 15M)// ② 增加了级别 3D效果(18 19)mapview = (MapView) findViewById(R.id.mapView);baiduMap = mapview.getMap();// BaiduMap: 管理具体的某一个MapView : 旋转,移动,缩放,事件。。// 描述地图状态将要发生的变化 使用工厂类MapStatusUpdateFactory创建MapStatusUpdate mapstatusUpdate = MapStatusUpdateFactory.zoomTo(15);// 默认的级别12// 设置缩放级别baiduMap.setMapStatus(mapstatusUpdate);// LatLng latlng = new LatLng(arg0, arg1);// 坐标 经纬度 参数1 纬度 参数2 经度MapStatusUpdate mapstatusUpdatePoint = MapStatusUpdateFactory.newLatLng(hmPos);// 设置中心点 默认是学校baiduMap.setMapStatus(mapstatusUpdatePoint);// mapview.showZoomControls(false);// 默认是true 显示缩放按钮//// mapview.showScaleControl(false);// 默认是true 显示标尺}@Overrideprotected void onDestroy() {mapview.onDestroy();super.onDestroy();}@Overrideprotected void onResume() {mapview.onResume();super.onResume();}@Overrideprotected void onPause() {mapview.onPause();super.onPause();}
}

六.展示圆形覆盖物

public class CircleOptionsDemo extends BaseActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);draw();}private void draw() {CircleOptions circleOptions = new CircleOptions();circleOptions.center(hmPos).radius(100).fillColor(0X66FF0000);baiduMap.addOverlay(circleOptions);}
}

七.显示文字

public class TextOptionsDemo extends BaseActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);draw();}private void draw() {TextOptions options = new TextOptions();options.fontColor(0x60FF0000).text("老罗的家") // 文字内容.position(hmPos) // 位置.fontSize(50)// 字体大小.typeface(Typeface.SANS_SERIF)// 字体.rotate(30);// 旋转baiduMap.addOverlay(options);}
}

八.展示图片在特地位置(Mark覆盖物)

public class MarkerOptionsDemo extends BaseActivity {private View pop;private MarkerOptions markerOptions;private TextView title;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);draw();initPop();// 点击某一个Marker 在脑袋上弹出pop}private void initPop() {pop = View.inflate(getApplicationContext(), R.layout.pop, null);LayoutParams params = new MapViewLayoutParams.Builder().layoutMode(MapViewLayoutParams.ELayoutMode.mapMode)// 按照经纬度设置位置.position(hmPos)// 不能传null 设置为mapMode时 必须设置position.width(MapViewLayoutParams.WRAP_CONTENT).height(MapViewLayoutParams.WRAP_CONTENT).build();mapview.addView(pop,params );pop.setVisibility(View.INVISIBLE);title = (TextView) pop.findViewById(R.id.title);}private void draw() {BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.eat_icon);BitmapDescriptor bitmap2 = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);ArrayList<BitmapDescriptor> bitmaps = new ArrayList<BitmapDescriptor>();bitmaps.add(bitmap);bitmaps.add(bitmap2);markerOptions = new MarkerOptions();markerOptions.draggable(true).title("老罗").position(hmPos).icons(bitmaps).period(10);// 设置多少帧刷新一次图片资源,Marker动画的间隔时间,值越小动画越快baiduMap.addOverlay(markerOptions);markerOptions= new MarkerOptions().title("向北").position(new LatLng(latitude + 0.001, longitude)).icon(bitmap);baiduMap.addOverlay(markerOptions);markerOptions= new MarkerOptions().title("向东").position(new LatLng(latitude , longitude+ 0.001)).icon(bitmap);baiduMap.addOverlay(markerOptions);markerOptions= new MarkerOptions().title("向西南").position(new LatLng(latitude - 0.001, longitude - 0.001)).icon(bitmap);baiduMap.addOverlay(markerOptions);baiduMap.setOnMarkerClickListener(new MyListener());}class MyListener implements OnMarkerClickListener{@Overridepublic boolean onMarkerClick(Marker result) {// 当点击时 更新pop的位置 设置为显示LayoutParams params = new MapViewLayoutParams.Builder().layoutMode(MapViewLayoutParams.ELayoutMode.mapMode)// 按照经纬度设置位置.position(result.getPosition())// 不能传null.width(MapViewLayoutParams.WRAP_CONTENT).height(MapViewLayoutParams.WRAP_CONTENT).yOffset(-10)// 距离position的像素 向下是正值 向上是负值.build();//更新位置mapview.updateViewLayout(pop, params);pop.setVisibility(View.VISIBLE);title.setText(result.getTitle());return true;}}
}

九.矩形范围内搜索加油站

public class PoiSearchInBoundsDemo extends BaseActivity {private PoiSearch poiSearch;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);search();}private void search() {poiSearch = PoiSearch.newInstance();PoiBoundSearchOption poiboundSearch = new PoiBoundSearchOption();LatLngBounds latlngbounds = new LatLngBounds.Builder()
//      public double latitude = 30.472473;// 纬度
//      public double longitude = 114.418332;// 经度.include(new LatLng(30.480073,114.421132)).include(new LatLng(30.495673,114.434332)).build();poiboundSearch.bound(latlngbounds);poiboundSearch.keyword("加油站");poiSearch.searchInBound(poiboundSearch);//当搜索到信息时,回调这个监听poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {@Overridepublic void onGetPoiResult(PoiResult result) {if(result==null||SearchResult.ERRORNO.RESULT_NOT_FOUND==result.error){Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show();return;}//             PoiOverlay overlay = new PoiOverlay(baiduMap);PoiOverlay overlay = new MyOverlay(baiduMap);//设置点击事件baiduMap.setOnMarkerClickListener(overlay);//将点击事件传递给下面的overlayoverlay.setData(result);// 设置POI数据overlay.addToMap();//将所有Overlay 添加到地图上overlay.zoomToSpan();//缩放地图,使所有Overlay都在合适的视野内}@Overridepublic void onGetPoiDetailResult(PoiDetailResult result) {}});}class MyOverlay extends PoiOverlay{public MyOverlay(BaiduMap arg0) {super(arg0);}@Overridepublic boolean onPoiClick(int index) {PoiResult poiResult = getPoiResult();PoiInfo poiInfo = poiResult.getAllPoi().get(index);String info = poiInfo.city + ":" +poiInfo.name + ":" + poiInfo.address;Toast.makeText(getApplicationContext(), info, 0).show();return super.onPoiClick(index);}}
}

十.圆形范围内搜索加油站

//周边的搜索
public class PoiSearchNearbyDemo extends BaseActivity {private PoiSearch poiSearch;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);search();}private void search() {poiSearch = PoiSearch.newInstance();PoiNearbySearchOption nearbySearch = new PoiNearbySearchOption();nearbySearch.location(hmPos);nearbySearch.radius(1500);nearbySearch.keyword("加油站");poiSearch.searchNearby(nearbySearch);//当搜索到信息时,回调这个监听poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {//poi 查询结果回调@Overridepublic void onGetPoiResult(PoiResult result) {if(result==null||SearchResult.ERRORNO.RESULT_NOT_FOUND==result.error){Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show();return;}//              PoiOverlay overlay = new PoiOverlay(baiduMap);PoiOverlay overlay = new MyOverlay(baiduMap);//设置点击事件baiduMap.setOnMarkerClickListener(overlay);//将点击事件传递给下面的overlayoverlay.setData(result);// 设置POI数据overlay.addToMap();//将所有Overlay 添加到地图上overlay.zoomToSpan();//缩放地图,使所有Overlay都在合适的视野内}//poi 详情查询结果回调@Overridepublic void onGetPoiDetailResult(PoiDetailResult result) {if(result==null||SearchResult.ERRORNO.RESULT_NOT_FOUND==result.error){Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show();return;}String text = result.getAddress()+ "::" + result.getCommentNum() + result.getFacilityRating();Toast.makeText(getApplicationContext(), text, 0).show();}});}class MyOverlay extends PoiOverlay{public MyOverlay(BaiduMap arg0) {super(arg0);}@Overridepublic boolean onPoiClick(int index) {PoiResult poiResult = getPoiResult();PoiInfo poiInfo = poiResult.getAllPoi().get(index);String info = poiInfo.city + ":" +poiInfo.name + ":" + poiInfo.address;Toast.makeText(getApplicationContext(), info, 0).show();//在点击事件中添加详情uidPoiDetailSearchOption detailSearchOption = new PoiDetailSearchOption();detailSearchOption.poiUid(poiInfo.uid);// 设置poi的uidpoiSearch.searchPoiDetail(detailSearchOption);return super.onPoiClick(index);}}
}

十一.全城搜索加油站(南京,一个城市有很多.进行分页)

public class PoiSearchCityDemo extends BaseActivity {private PoiSearch poiSearch;private int currentPageIndex = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);search();}private void search() {poiSearch = PoiSearch.newInstance();PoiCitySearchOption poicitySearch = new PoiCitySearchOption();poicitySearch.city("南京");poicitySearch.keyword("加油站");poicitySearch.pageNum(currentPageIndex);poiSearch.searchInCity(poicitySearch );//当搜索到信息时,回调这个监听poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {@Overridepublic void onGetPoiResult(PoiResult result) {if(result==null||SearchResult.ERRORNO.RESULT_NOT_FOUND==result.error){Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show();return;}String text = "共" + result.getTotalPageNum() + "页,共"+ result.getTotalPoiNum() + "条,当前第"+ result.getCurrentPageNum() + "页,当前页"+ result.getCurrentPageCapacity() + "条";Toast.makeText(getApplicationContext(), text, 1).show();baiduMap.clear();// 清空地图所有的 Overlay 覆盖物以及 InfoWindow//             PoiOverlay overlay = new PoiOverlay(baiduMap);PoiOverlay overlay = new MyOverlay(baiduMap);//设置点击事件baiduMap.setOnMarkerClickListener(overlay);//将点击事件传递给下面的overlayoverlay.setData(result);// 设置POI数据overlay.addToMap();//将所有Overlay 添加到地图上overlay.zoomToSpan();//缩放地图,使所有Overlay都在合适的视野内}@Overridepublic void onGetPoiDetailResult(PoiDetailResult result) {}});}class MyOverlay extends PoiOverlay{public MyOverlay(BaiduMap arg0) {super(arg0);}@Overridepublic boolean onPoiClick(int index) {PoiResult poiResult = getPoiResult();PoiInfo poiInfo = poiResult.getAllPoi().get(index);String info = poiInfo.city + ":" +poiInfo.name + ":" + poiInfo.address;Toast.makeText(getApplicationContext(), info, 0).show();return super.onPoiClick(index);}}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode==KeyEvent.KEYCODE_1){currentPageIndex++;search();}return super.onKeyDown(keyCode, event);}
}

十二.驾车路线

//驾车路线图
public class DrivingRouteOverlayDemo extends BaseActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);search();}private void search() {//1.创建实例RoutePlanSearch routePlanSearch = RoutePlanSearch.newInstance();DrivingRoutePlanOption drivingRoutePlanOption = new DrivingRoutePlanOption();PlanNode from = PlanNode.withLocation(hmPos);// 创建起点;drivingRoutePlanOption.from(from);//设置路途中经过的点List<PlanNode> node = new ArrayList<PlanNode>();node.add(PlanNode.withCityNameAndPlaceName("武汉", "光谷广场"));drivingRoutePlanOption.passBy(node);PlanNode to = PlanNode.withLocation(new LatLng(30.422473, 114.418932));drivingRoutePlanOption.to(to);drivingRoutePlanOption.policy(DrivingRoutePlanOption.DrivingPolicy.ECAR_DIS_FIRST);// 设置策略// 2.加载routePlanSearch.drivingSearch(drivingRoutePlanOption);//3.设置驾车路线监听routePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {@Overridepublic void onGetWalkingRouteResult(WalkingRouteResult arg0) {}@Overridepublic void onGetTransitRouteResult(TransitRouteResult arg0) {}@Overridepublic void onGetDrivingRouteResult(DrivingRouteResult result) {if (result == null|| SearchResult.ERRORNO.RESULT_NOT_FOUND == result.error) {Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show();return;}DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(baiduMap);baiduMap.setOnMarkerClickListener(drivingRouteOverlay);drivingRouteOverlay.setData(result.getRouteLines().get(0));// 路线为第一条,有很多条drivingRouteOverlay.zoomToSpan();drivingRouteOverlay.addToMap();}});}class MyDrivingOverlay extends DrivingRouteOverlay {public MyDrivingOverlay(BaiduMap arg0) {super(arg0);}//起始点的图标设置@Overridepublic BitmapDescriptor getStartMarker() {return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);}//终点的图标设置@Overridepublic BitmapDescriptor getTerminalMarker() {// TODO Auto-generated method stubreturn BitmapDescriptorFactory.fromResource(R.drawable.icon_en);}}
}

十三.步行路线

/*** 步行路线* @author h**/
public class WalkingRouteOverlayDemo extends BaseActivity {private RoutePlanSearch routePlanSearch;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);search();}private void search() {//1.创建实例routePlanSearch = RoutePlanSearch.newInstance();WalkingRoutePlanOption walkOption = new WalkingRoutePlanOption();PlanNode from = PlanNode.withLocation(hmPos);// 创建起点PlanNode to = PlanNode.withLocation(new LatLng(30.422473, 114.418932));// 创建终点walkOption.from(from);walkOption.to(to);//2.设置步行routePlanSearch.walkingSearch(walkOption);//3.设置监听routePlanSearch.setOnGetRoutePlanResultListener(new MyListener());}class MyListener implements OnGetRoutePlanResultListener{@Overridepublic void onGetDrivingRouteResult(DrivingRouteResult result) {}@Overridepublic void onGetTransitRouteResult(TransitRouteResult result) {}//监听结果@Overridepublic void onGetWalkingRouteResult(WalkingRouteResult result) {if (result == null|| SearchResult.ERRORNO.RESULT_NOT_FOUND == result.error) {Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show();return;}//展示OverlayWalkingRouteOverlay overlay = new WalkingRouteOverlay(baiduMap);baiduMap.setOnMarkerClickListener(overlay);// 把事件传递给overlayoverlay.setData(result.getRouteLines().get(0));// 设置线路为第一条overlay.addToMap();overlay.zoomToSpan();}}
}

十四.公交换乘

/*** 公交换乘路线* @author h**/
public class TransitRouteOverlayDemo extends BaseActivity {private RoutePlanSearch routePlanSearch;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);search();}private void search() {//1.创建实例routePlanSearch = RoutePlanSearch.newInstance();TransitRoutePlanOption transitOption = new TransitRoutePlanOption();PlanNode from = PlanNode.withLocation(hmPos);// 创建起点PlanNode to = PlanNode.withLocation(new LatLng(30.422473, 114.418932));// 创建终点transitOption.from(from);transitOption.to(to);transitOption.city("武汉");transitOption.policy(TransitRoutePlanOption.TransitPolicy.EBUS_WALK_FIRST);//2.设置公交routePlanSearch.transitSearch(transitOption);//3.设置监听routePlanSearch.setOnGetRoutePlanResultListener(new MyListener());}class MyListener implements OnGetRoutePlanResultListener{@Overridepublic void onGetDrivingRouteResult(DrivingRouteResult result) {}//路线结果@Overridepublic void onGetTransitRouteResult(TransitRouteResult result) {if (result == null|| SearchResult.ERRORNO.RESULT_NOT_FOUND == result.error) {Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show();return;}TransitRouteOverlay overlay = new TransitRouteOverlay(baiduMap);baiduMap.setOnMarkerClickListener(overlay);// 把事件传递给overlayoverlay.setData(result.getRouteLines().get(0));// 设置线路为第一条overlay.addToMap();overlay.zoomToSpan();}@Overridepublic void onGetWalkingRouteResult(WalkingRouteResult result) {}}class MyDrivingOverlay extends DrivingRouteOverlay{public MyDrivingOverlay(BaiduMap arg0) {super(arg0);}@Overridepublic BitmapDescriptor getStartMarker() {//覆写此方法以改变默认起点图标return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);}@Overridepublic BitmapDescriptor getTerminalMarker() {//覆写此方法以改变默认终点图标return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);}}
}

十五.定位

public class LocationDemo extends BaseActivity {private LocationClient mLocationClient;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);locate();}private void locate() {BDLocationListener myListener = new MyLocationListener();mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类mLocationClient.registerLocationListener(myListener); // 注册监听函数initLocation();}private void initLocation() {LocationClientOption option = new LocationClientOption();option.setLocationMode(LocationMode.Hight_Accuracy);// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备option.setIgnoreKillProcess(false);option.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系int span = 1000;option.setScanSpan(span);// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要//option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向//option.setIsNeedLocationDescribe(true);//// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”// option.setIsNeedLocationPoiList(true);//// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到//option.setIgnoreKillProcess(false);// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死//option.SetIgnoreCacheException(false);// 可选,默认false,设置是否收集CRASH信息,默认收集// option.setEnableSimulateGps(false);// 可选,默认false,设置是否需要过滤GPS仿真结果,默认需要mLocationClient.setLocOption(option);BitmapDescriptor geo = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);MyLocationConfiguration configuration = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.COMPASS, true, geo);//NORMAL FOLLOWING COMPASS三种模式baiduMap.setMyLocationConfigeration(configuration);// 设置定位显示的模式baiduMap.setMyLocationEnabled(true);// 打开定位图层}class MyLocationListener implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation result) {if (result != null) {MyLocationData data = new MyLocationData.Builder().latitude(result.getLatitude()).longitude(result.getLongitude()).build();baiduMap.setMyLocationData(data);}}}@Overrideprotected void onStart() {mLocationClient.start();super.onStart();}@Overrideprotected void onPause() {mLocationClient.stop();super.onPause();}
}

6.百度地图例子讲解相关推荐

  1. Android studio百度地图SDK开发 2020最新超详细的Android 百度地图开发讲解(3) 路线规划步行骑行驾车路线规划

    2020最新超详细的Android 百度地图开发讲解(3) 路线规划步行骑行驾车路线规划 开发前配置,显示基本地图,实时定位等查看之前代码,此博客紧接上一博客:https://blog.csdn.ne ...

  2. android百度地图例子,使用百度地图API进行Android地图应用开发(Eclipse)(示例代码)...

    随着基于位置的服务的兴起,地图类App呈现爆发趋势.随着而来的是地图供应商开放大量的API.供开发人员开发基于PC或者移动端的应用程序. 如今我们研究使用百度地图SDK进行Android项目的开发. ...

  3. Android studio百度地图SDK开发 2020最新超详细的Android 百度地图开发讲解(6) POI检索, 根据地址输入提示检索 Sug

    POI检索, 根据地址输入提示检索 Sug 参考百度地图官方文档:http://lbsyun.baidu.com/index.php?title=androidsdk/guide/search/sug ...

  4. android百度地图例子,Android使用百度地图的注意点

    /* http://my.oschina.net/lifj/blog/283063 */ 0.不要参考官方的网页示例 很奇怪,他的官网的使用示例更新不及时. 下载api的时候,里面有一个demo.最好 ...

  5. android studio百度地图教程,Android studio百度地图SDK开发 2020最新超详细的Android 百度地图开发讲解(4) 路线规划之起始点地址输入实现规划...

    实现起始点输入的路线规划 要实现输入起始点的路线规划,需要两个输入框,可以将两个输入框设置在和地图同一个Activity中,也可以新建一个Activity,来实现地址的输入,然后通过页面之间的跳转来传 ...

  6. android地图入门,android 百度地图入门01 (史上最详没有之一)

    最近一直和百度地图打交道,写几篇博客记录一下吧,目前最新版是4.0的 ,之前我用的是3.7的, 就以4.0的为例说一下最基本的配置流程吧. 一.准备工作 1.申请一个百度地图开发者账户--地址:htt ...

  7. 百度地图添加自定义Marker+显示覆盖层次优先级

    转载请注明出处:blog.csdn.net/wll995806658 实际项目中经常会集成地图,今天就只谈谈百度地图添加自定义Marker这一部分,希望对百度地图集成有这部分需求的小伙伴能有一些帮助. ...

  8. 最新百度地图—POI搜索功能讲解

    这几天都在学习百度地图的事例Demo,后续都会有其它功能的讲解: 对了  我们这里的最新只是代指现在哦!百度地图更新的很快的,最好还是看百度api最权威. 由于上一篇已经讲解过注册文件,这里我就不写那 ...

  9. Vue+Echarts+百度地图 小例子

    刚学完Echarts后,看到一些官方例子,粘贴代码下来却实现不了相应的效果,经过一番了解,发现还要引入百度地图,记录详细过程如下. 1.安装echarts(使用3.x,4.x) npm install ...

最新文章

  1. SQLMAP注入教程-11种常见SQLMAP使用方法
  2. 对SqlCommandBuilder类理解
  3. 【面试题】struts2的工作流程
  4. 为什么不用ZK来做服务发现?
  5. 进程kswapd0与events/0消耗大量CPU的问题
  6. dspmq dspmqver command not found(dspmq命令找不到,dspmqver主安装目录设置不正确
  7. 老马的原创空间搬家通告
  8. 每日算法系列【LeetCode 1031】两个非重叠子数组的最大和
  9. jQuery Deferred对象
  10. Nature报道诺奖得主:给研究生的四条箴言 Four golden lessons。颜宁:写的真好!
  11. 云服务器代理平台安装教程
  12. 【操作系统】进程:管程
  13. 20150317 实习之——余世维视频(上)
  14. python获取文件夹名称、文件名、去除后缀的文件名、文件改名等
  15. 通过jmeter进行用户并发(vu/s)测试
  16. 马哥架构第4周课程作业
  17. 海康摄像头中的SADP协议(Smart Active Device Protocol,智能活动设备协议)和ONVIF协议(Open Network Video Interface Forum)是什么?
  18. 河北工程技术学院计算机应用技术,2020年春河北工程技术学院期末考试计算机应用基础试题及答案(13页)-原创力文档...
  19. 【DFS】不撞南墙不回头—深度优先搜索算法[Deep First Search]
  20. tuneup utilities 2012怎么用?全是英文 怎么使用?

热门文章

  1. python的invalid syntax是什么意思_python 中 出现 invalid syntax 是什么意思 我是新手 求告知。...
  2. topsis法matlab程序,TOPSIS算法(示例代码)
  3. 摄影镜头调制传输函数MTF解读
  4. Linux CentOS 8常见命令
  5. 马尔萨斯人口论与数学模型
  6. CT值(亨氏值H) 孔隙率 像素点 灰度值 RGB
  7. 【 C++11 】列表初始化、声明、范围for、STL中一些变化
  8. 方向导数、梯度、等高线、数量场与向量场
  9. Appinum 自动化测试利器入门
  10. 各种说明方法的答题格式_12种说明方法的答题格式