百度地图移动版API(Android)是一套基于Android设备的应用程序接口,通过该接口,可以轻松的访问百度服务和数据,构建功能丰富、交互性强的地图应用程序。百度地图移动版API不仅包含构建地图的基本接口,还提供了本地搜索、路线规划、地图定位等数据服务
在Preference中可以看到Android项目Eclipse默认的数字签名SHA1
切换到模拟器安装目录下输入命令 命令 "C:/Program Files/Java/jdk1.6.0_45/bin/keytool.exe" -list -v -keystoredebug.keystore
默认密码是 android
也能获取数字签名的SHA1
4.添加权限
<!-- 这个权限用于进行网络定位--> <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>
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="33c6iZyFukuzDOg77Wk6wE2t"/>
public class HelloWorld extends Activity{ private static final String TAG ="HelloWorld"; private MapView mapView; private BaiduMap baiduMap; private double latitude = 40.050966;// 纬度 private double longitude = 116.303128;// 经度 private LatLng hmPos = new LatLng(latitude, longitude);// 坐标 private MyBaiduSdkReceiver receiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); intManager(); setContentView(R.layout.common); init(); } private void intManager() { receiver = new MyBaiduSdkReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR); filter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR); registerReceiver(receiver, filter); } private void init() { mapView = (MapView) findViewById(R.id.mapView); //BaiduMap管理具体的某一个MapView: 旋转,移动,缩放,事件。。。 baiduMap = mapView.getMap(); //设置缩放级别,默认级别为12 MapStatusUpdate mapstatusUpdate = MapStatusUpdateFactory.zoomTo(19);; baiduMap.setMapStatus(mapstatusUpdate); //设置地图中心点,默认是天安门 MapStatusUpdate mapstatusUpdatePoint = MapStatusUpdateFactory.newLatLng(hmPos); baiduMap.setMapStatus(mapstatusUpdatePoint ); //mapView.showScaleControl(false);//默认是true,显示缩放按钮 //mapView.showZoomControls(false);//默认是true,显示比例尺 } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //旋转,移动,缩放 switch (keyCode) { case KeyEvent.KEYCODE_1: //放大地图缩放级别,每次放大一个级别 baiduMap.setMapStatus(MapStatusUpdateFactory.zoomIn()); break; case KeyEvent.KEYCODE_2: //每次缩小一个级别 baiduMap.setMapStatus(MapStatusUpdateFactory.zoomOut()); break; case KeyEvent.KEYCODE_3: //以一个点为中心旋转 //获取地图当前的状态 MapStatus mapStatus = baiduMap.getMapStatus(); float rotate = mapStatus.rotate; Log.d(TAG, "rotate:" + rotate); //旋转范围 0-360 MapStatus newRotate = new MapStatus.Builder().rotate(rotate+30).build(); baiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(newRotate)); break; case KeyEvent.KEYCODE_4: //以一条直线为轴,旋转 调整俯仰角 overlook //范围 0-45 float overlook = baiduMap.getMapStatus().overlook; MapStatus overStatus = new MapStatus.Builder().overlook(overlook-5).build(); baiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(overStatus)); break; case KeyEvent.KEYCODE_5: //移动 MapStatusUpdate moveStatusUpdate = MapStatusUpdateFactory.newLatLng(new LatLng(40.065796,116.349868)); //带动画的更新地图状态,还是300毫秒 baiduMap.animateMapStatus(moveStatusUpdate); break; default: break; } return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { //卸载super的前后是没有却别的 mapView.onDestroy(); unregisterReceiver(receiver); super.onDestroy(); } @Override protected void onResume() { mapView.onResume(); super.onResume(); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); mapView.onPause(); } class MyBaiduSdkReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR)) { //网络错误 Toast.makeText(getApplicationContext(), "无网络", 0).show(); }else if (action.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)) { //key校验失败 Toast.makeText(getApplicationContext(), "校验失败", 0).show(); } } } }
地图可以包含一个或多个图层,每个图层在每个级别都是由若干个图块组成的,他们覆盖了地图的整个表面。例如您所看到的包括街道、兴趣点(POI)、学校、公园等内容的地图展现就是一个图层,另外交通留香的展现也是通过图层来实现的。
baiduMap.setTrafficEnabled(true)
baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE)
卫星地图是卫星拍摄的真实的地理面貌(其实很多都是飞机航拍的,因为卫星拍照贵啊),所以卫星地图可用来检测地面的信息
覆盖物的成绩压盖关系,具体如下(从下至上的顺序):
1.基础底图(包括底图、底图道路、卫星图等);
2.地形图图层(GroundOverlay);
3.热力图图层(HeatMap);
4.实时路况图图层(BaiduMap.setTrafficEnabled(true););
5.百度城市热力图(BaiduMap.setBaiduHeatMapEnabled(true););
6.底图标注(指的是底图上面自带的那些POI元素);
7.几何图形图层(点、折线、弧线、圆、多边形);
8.标注图层(Marker),文字绘制图层(Text);
9.指南针图层(当地图发生旋转和视角变化时,默认出现在左上角的指南针);
10.定位图层(BaiduMap.setMyLocationEnabled(true););
11.弹出窗图层(InfoWindow);
12.自定义View(MapView.addView(View););
切换地图,交通图,卫星图,是否显示实时交通
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { //切换 地图、交通图、 卫星图 switch (keyCode) { case KeyEvent.KEYCODE_1: //按1 设置显示底图 baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); break; case KeyEvent.KEYCODE_2: //卫星图 baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); break; case KeyEvent.KEYCODE_3: //打开交通图 baiduMap.setTrafficEnabled(true); break; } return super.onKeyDown(keyCode, event); }
所有叠加或覆盖到地图的内容,我们琮琤为地图覆盖物。如标注、矢量图性元素(包括:折现和多边形和圆),定位图标等。覆盖物拥有自己的地理坐标,当您拖动或缩放地图时,他们会向应的处理
#### 本地覆盖物的抽象基类:OverlayOptions(核心类)
弧线覆盖物:ArcOptions
公交路线覆盖物:BusLineOverlay
private void draw() { //定义一个圆 : 圆心+半径 //1.创建自己 CircleOptions circleOptions = new CircleOptions(); //2.给自己设置数据 circleOptions.center(hmPos) //圆心 .radius(1000)//半径 单位米 .fillColor(0x60ff0000)//填充色 .stroke(new Stroke(2,0x6000ff00));//边框宽度和颜色 //3.把覆盖物添加到地图中 baiduMap.addOverlay(circleOptions); }
效果
添加文字覆盖物
private void draw() { TextOptions textOptions = new TextOptions(); textOptions.fontColor(0x60ff0000)//设置字体颜色 .text("无良印品,窗前明月光")//文字内容 .position(hmPos)//位置 .fontSize(24)//字体大小 .typeface(Typeface.SERIF)//字体 .rotate(30);//旋转 baiduMap.addOverlay(textOptions); }
效果
Marker覆盖物
public class MarkerOptionsDemo extends BaseActivity { private View pop; private TextView title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); draw(); //点击某一个Marker,在脑袋上弹出pop initPop(); } private void draw() { BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.eat_icon); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(hmPos)//设置位置 .icon(bitmapDescriptor)//设置覆盖物小图标 .draggable(true)//设置是否可以拖拽,默认为否 .title("中心");//设置标题 baiduMap.addOverlay(markerOptions); markerOptions = new MarkerOptions().title("向北") .position(new LatLng(latitude+0.001, longitude)) .icon(bitmapDescriptor); baiduMap.addOverlay(markerOptions); ArrayList<BitmapDescriptor> bitmaps = new ArrayList<BitmapDescriptor>(); bitmaps.add(bitmapDescriptor); bitmaps.add(BitmapDescriptorFactory.fromResource(R.drawable.icon_geo)); markerOptions = new MarkerOptions().title("向东") .position(new LatLng(latitude, longitude+0.001)) .icons(bitmaps)//图标设置一个帧动画,多个图片来回切换 .period(10);//切换帧的时间间隔 baiduMap.addOverlay(markerOptions); markerOptions = new MarkerOptions().title("向西南") .position(new LatLng(latitude-0.001, longitude-0.001)) .icon(bitmapDescriptor); baiduMap.addOverlay(markerOptions); baiduMap.setOnMarkerClickListener(new OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker result) { //当点击时,更新pop的位置,设置为显示 MapViewLayoutParams layoutParams = new MapViewLayoutParams.Builder() .layoutMode(ELayoutMode.mapMode)//按照经纬度设置位置 .position(result.getPosition())//不能传null .width(MapViewLayoutParams.WRAP_CONTENT) .height(MapViewLayoutParams.WRAP_CONTENT) .yOffset(-5)//距离position的像素 向下是正值,向上是负值 .build(); mapview.updateViewLayout(pop, layoutParams); pop.setVisibility(View.VISIBLE); title.setText(result.getTitle()); return true; } }); } private void initPop() { //加载pop 添加到mapview,设置为隐藏 pop = View.inflate(getApplicationContext(), R.layout.pop, null); MapViewLayoutParams layoutParams = new MapViewLayoutParams.Builder() .layoutMode(MapViewLayoutParams.ELayoutMode.mapMode)//按照经纬度设置位置 .position(hmPos)//不能传null,设置为mapMode时,必须设置position .width(MapViewLayoutParams.WRAP_CONTENT) .height(MapViewLayoutParams.WRAP_CONTENT) .build(); mapview.addView(pop, layoutParams); pop.setVisibility(View.INVISIBLE); title = (TextView) pop.findViewById(R.id.title); } }
百度地图移动版API集成搜索服务,包括位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索
PoiSearch和OnGetPoiSearchResultListener RoutePlanSearch和OnGetRoutePlanResultListener
注意,OnGetPoiSearchResultListener只支持一个,一最后一次设置为准
本地搜索覆盖物:PoiOverlay
驾车路线覆盖物:DrivingRouteOverlay
步行路线覆盖物:WalkingRouteOverlay
换乘路线覆盖物:TransitOverlay
根据范围和检索词发起范围检索searchInBound
周边检索searchNearby
城市poi检索searchInCity
poi详细信息检索 searchPoiDetail
结果的展示: PoiOverlay
翻页功能重新调用通过PoiSearch类的searchInXxx()方法并传递页数角标pageNum来实现,该方法是异步函数,搜索成功后会调用注册的事件处理函数onGetPoiResult 返回查询页的结果
在一定范围内搜索POI
/** * 范围内搜索 * @author mwp * */ public class PoiSearchInBoundsDemo extends BaseActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); search(); } private void search() { PoiSearch poiSearch = PoiSearch.newInstance(); PoiBoundSearchOption boundSearchOption = new PoiBoundSearchOption(); LatLngBounds latLngBounds = new LatLngBounds.Builder() .include(new LatLng(40.049233, 116.302675))//东北的点 .include(new LatLng(40.050645, 116.303695))//西南的点 .build(); //由东北的点和西南的点两个点确定搜索的矩形范围 boundSearchOption.bound(latLngBounds);//设置搜索范围 boundSearchOption.keyword("加油站");//搜索关键字 poiSearch.searchInBound(boundSearchOption);// 只是把范围能搜索到的poi设置为 地图的中心,同时会搜索到范围外的poi poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() { @Override public void onGetPoiResult(PoiResult result) { if (result==null || result.error==ERRORNO.RESULT_NOT_FOUND) { Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show(); return; } //搜索poi的覆盖物 MyPoiOverlay overlay = new MyPoiOverlay(baiduMap); //把事件分发给overlay,overlay才能处理那个点击事件 baiduMap.setOnMarkerClickListener(overlay); overlay.setData(result);//设置结果 overlay.addToMap();//把搜索的结果添加到地图中 overlay.zoomToSpan();//缩放地图,使所有overlay都在合适的视野内, 注: 该方法只对Marker类型的overlay有效 } @Override public void onGetPoiDetailResult(PoiDetailResult result) { } }); } class MyPoiOverlay extends PoiOverlay{ public MyPoiOverlay(BaiduMap arg0) { super(arg0); } @Override public boolean onPoiClick(int index) { //获取搜索结果 PoiResult poiResult = getPoiResult(); //获取点击的那个poi的信息 PoiInfo poiInfo = poiResult.getAllPoi().get(index); String text = poiInfo.name+","+poiInfo.address; Toast.makeText(getApplicationContext(), text, 0).show(); return super.onPoiClick(index); } } }
搜索结果
周边搜索
/** * 周边搜索 * @author mwp * */ public class PoiSearchNearByDemo extends BaseActivity{ private PoiSearch poiSearch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); search(); } private void search() { poiSearch = PoiSearch.newInstance(); PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption(); nearbySearchOption.location(hmPos);//设置中心点 nearbySearchOption.radius(1000);//设置半径 米 nearbySearchOption.keyword("加油站");//关键字 poiSearch.searchNearby(nearbySearchOption); poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() { @Override public void onGetPoiResult(PoiResult result) { if(result==null||SearchResult.ERRORNO.RESULT_NOT_FOUND==result.error){ Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show(); return; } PoiOverlay overlay = new MyPoiOverlay(baiduMap);// 搜索poi的覆盖物 baiduMap.setOnMarkerClickListener(overlay);// 把事件分发给overlay,overlay才能处理点击事件 overlay.setData(result);// 设置结果 overlay.addToMap();// 把搜索的结果添加到地图中 overlay.zoomToSpan();// 缩放地图,使所有Overlay都在合适的视野内 注: 该方法只对Marker类型的overlay有效 } @Override public 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.getEnvironmentRating(); Toast.makeText(getApplicationContext(), text, 0).show(); } }); } class MyPoiOverlay extends PoiOverlay{ public MyPoiOverlay(BaiduMap arg0) { super(arg0); } @Override public boolean onPoiClick(int index) { PoiResult poiResult = getPoiResult(); PoiInfo poiInfo = poiResult.getAllPoi().get(index);// 得到点击的那个poi信息 String text = poiInfo.name + "," + poiInfo.address; Toast.makeText(getApplicationContext(), text, 0).show(); PoiDetailSearchOption detailSearchOption = new PoiDetailSearchOption(); detailSearchOption.poiUid(poiInfo.uid);//设置poi的uid poiSearch.searchPoiDetail(detailSearchOption); return super.onPoiClick(index); } } }
搜索结果
城市内搜索
/** * 城市内搜索 * @author mwp * */ public class PoiSearchIncityDemo extends BaseActivity { private PoiSearch poiSearch; private int currentPageIndex = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); poiSearch = PoiSearch.newInstance(); search(); poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() { @Override public 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 MyPoiOverlay(baiduMap); baiduMap.setOnMarkerClickListener(overlay); overlay.setData(result); overlay.addToMap(); overlay.zoomToSpan(); } @Override public void onGetPoiDetailResult(PoiDetailResult arg0) { } }); } private void search() { PoiCitySearchOption citySearchOption = new PoiCitySearchOption(); citySearchOption.city("北京"); citySearchOption.keyword("加油站"); citySearchOption.pageNum(currentPageIndex); poiSearch.searchInCity(citySearchOption); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { //每按一下数字键1就显示下一页的POI if (keyCode == KeyEvent.KEYCODE_1) { currentPageIndex++; search(); } return super.onKeyDown(keyCode, event); } class MyPoiOverlay extends PoiOverlay{ public MyPoiOverlay(BaiduMap arg0) { super(arg0); } @Override public boolean onPoiClick(int index) { PoiResult poiResult = getPoiResult(); PoiInfo poiInfo = poiResult.getAllPoi().get(index);// 得到点击的那个poi信息 String text = poiInfo.name + "," + poiInfo.address; Toast.makeText(getApplicationContext(), text, 0).show(); return super.onPoiClick(index); } } }
结果展示: DrivingRouteOverlay
步行路线查询
结果展示: WalkingRouteOverlay
RoutePlanSearch. walkingSearch步行路线搜索.
结果处理: OnGetRoutePlanResultListener. onGetWalkingRouteResult(WalkingRouteResult result)
结果展示:TransitRouteOverlay
检索: RoutePlanSearch. transitSearch
通过TransitRoutePlanOption.policy (int policy) 设置路线规划策略
结果处理: OnGetRoutePlanResultListener. onGetTransitRouteResult(TransitRouteResult result)
/** * 驾车路线 * @author mwp * */ public class DrivingRouteOverlayDemo extends BaseActivity { private RoutePlanSearch routePlanSearch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); search(); } private void search() { routePlanSearch = RoutePlanSearch.newInstance(); DrivingRoutePlanOption drivingOption = new DrivingRoutePlanOption(); PlanNode from = PlanNode.withLocation(hmPos);//窗前起点 PlanNode to = PlanNode.withLocation(new LatLng(40.065796,116.349868));//创建终点 drivingOption.from(from);//设置起点 drivingOption.to(to);//设置终点 List<PlanNode> nodes = new ArrayList<PlanNode>(); nodes.add(PlanNode.withCityNameAndPlaceName("北京", "天安门")); drivingOption.passBy(nodes); drivingOption.policy(DrivingPolicy.ECAR_DIS_FIRST);//设置策略,驾乘检索策略常量:最短距离 routePlanSearch.drivingSearch(drivingOption); routePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() { @Override public void onGetWalkingRouteResult(WalkingRouteResult arg0) { } @Override public void onGetTransitRouteResult(TransitRouteResult arg0) { } // 自驾搜索结果回调 @Override public void onGetDrivingRouteResult(DrivingRouteResult result) { if (result == null || SearchResult.ERRORNO.RESULT_NOT_FOUND == result.error) { Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show(); return; } DrivingRouteOverlay drivingOverlay = new MyDrivingOverlay(baiduMap); baiduMap.setOnMarkerClickListener(drivingOverlay); drivingOverlay.setData(result.getRouteLines().get(0));//设置线路为搜索结果的第一条 drivingOverlay.addToMap(); drivingOverlay.zoomToSpan(); } }); } class MyDrivingOverlay extends DrivingRouteOverlay{ public MyDrivingOverlay(BaiduMap arg0) { super(arg0); } @Override public BitmapDescriptor getStartMarker() { // 覆写此方法以改变默认起点图标 return BitmapDescriptorFactory.fromResource(R.drawable.icon_st); } @Override public BitmapDescriptor getTerminalMarker() { // 覆写此方法以改变默认终点图标 return BitmapDescriptorFactory.fromResource(R.drawable.icon_en); } } }
搜索结果
步行线路规划
/** * 步行路线 * @author mwp * */ public class WalkingRouteOverlayDemo extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); search(); } private void search() { RoutePlanSearch routePlanSearch = RoutePlanSearch.newInstance(); WalkingRoutePlanOption walkingOption = new WalkingRoutePlanOption(); PlanNode from = PlanNode.withLocation(hmPos);//创建起点 PlanNode to = PlanNode.withLocation(new LatLng(40.065796,116.349868));// 创建终点 walkingOption.from(from); walkingOption.to(to); routePlanSearch.walkingSearch(walkingOption); routePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() { @Override public void onGetWalkingRouteResult(WalkingRouteResult result) { if (result == null || SearchResult.ERRORNO.RESULT_NOT_FOUND == result.error) { Toast.makeText(getApplicationContext(), "未搜索到结果", 0).show(); return; } WalkingRouteOverlay overlay = new WalkingRouteOverlay(baiduMap); baiduMap.setOnMarkerClickListener(overlay); overlay.setData(result.getRouteLines().get(0)); overlay.addToMap(); overlay.zoomToSpan(); } @Override public void onGetTransitRouteResult(TransitRouteResult arg0) { } @Override public void onGetDrivingRouteResult(DrivingRouteResult arg0) { } }); } }
搜索结果
公交换乘规划
/** * 公交换乘路线规划 * @author mwp * */ public class TransitRouteOverlayDemo extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); search(); } private void search() { RoutePlanSearch routePlanSearch = RoutePlanSearch.newInstance(); TransitRoutePlanOption transitOption = new TransitRoutePlanOption(); PlanNode from = PlanNode.withLocation(hmPos);// 创建起点 PlanNode to = PlanNode.withLocation(new LatLng(40.065796,116.349868));// 创建终点 transitOption.from(from); transitOption.to(to); transitOption.city("北京");//公交换乘只能在一个城市中,不能跨城市 transitOption.policy(TransitPolicy.EBUS_WALK_FIRST);//公交检索策略常量:最少步行距离 routePlanSearch.transitSearch(transitOption); routePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() { @Override public void onGetWalkingRouteResult(WalkingRouteResult arg0) { } @Override public 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); overlay.setData(result.getRouteLines().get(0)); overlay.addToMap(); overlay.zoomToSpan(); } @Override public void onGetDrivingRouteResult(DrivingRouteResult arg0) { } }); } class MyDrivingOverlay extends DrivingRouteOverlay{ public MyDrivingOverlay(BaiduMap arg0) { super(arg0); } @Override public BitmapDescriptor getStartMarker() { //覆写此方法以改变默认起点图标 return BitmapDescriptorFactory.fromResource(R.drawable.icon_st); } @Override public BitmapDescriptor getTerminalMarker() { //覆写此方法以改变默认终点图标 return BitmapDescriptorFactory.fromResource(R.drawable.icon_en); } } }
GeoCoder.geocode(GeoCodeOption option)
根据地址名获取地址信息 异步函数,返回结果在OnGetGeoCoderResultListener里的onGetGeoCodeResult方法通知
GeoCoder.reverseGeoCode(ReverseGeoCodeOption option)
根据地理坐标点获取地址信息 异步函数,返回结果在OnGetGeoCoderResultListener里的onGetReverseGeoCodeResult方法通知
SuggestionSearch . requestSuggestion(SuggestionSearchOption option)
查询一系列与指定key相关的内容,结果中包括城市及包含key的名称
结果处理OnGetSuggestionResultListener . onGetSuggestionResult(SuggestionResult result)
在百度地图移动版API中,提供一个重要的特色功能:定位,通过这个功能,能获取到用户当前所在位置。
在程序中,如果使用此功能,必须注册GPS和网络的使用权限。
在获取用户位置时,优先使用GPS进行定位;如果GPS定位没有打开或者没有可用位置信息,则会通过判断网络是否连接(即确认手机是否能上网,不论是连接2G/3G或Wi-Fi网络),如果是,则通过请求百度网络定位服务,返回网络定位结果。为了使获得的网络定位结果更加精确,请打开手机的Wi-Fi开关。
目前系统自带的网络定位服务精度低,且服务不稳定、精度低,并且从未来的趋势看,基站定位是不可控的(移动公司随时可能更改基站编号以垄断定位服务),而Wi-Fi定位则不然,它是一种精度更高、不受管制的定位方法。国内其它使用Wi-Fi定位的地图软件,Wi-Fi定位基本不可用,百度的定位服务量化指标优秀,网络接口返回速度快(服务端每次定位响应时间50毫秒以内),平均精度70米,其中Wi-Fi精度40米左右,基站定位精度200米左右,覆盖率98%,在国内处于一枝独秀的地位。
注意
关于经纬度的说明:该经纬度信息是经过加密处理,所以在其它地图工具中测得的经纬度 信息不适合百度的坐标系统。
使用百度经纬度坐标,可以通过http://api.map.baidu.com/lbsapi/getpoint/index.html查询地理坐标如果需要在百度地图上显示使用其他坐标系统的位置, 请发邮件至mapapi@baidu.com申请坐标转换接口http ://developer.baidu.com/map/index.php?title=android-locsdk/guide/v5-0
/** * 定位 * * @author mwp * */ public class LocationDemo extends BaseActivity { private LocationClient locationClient; private BitmapDescriptor geo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lacate(); } private void lacate() { locationClient = new LocationClient(getApplicationContext()); LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式 option.setCoorType("bd0911");// 返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms option.setIsNeedAddress(true);// 返回的定位结果包含地址信息 option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向 locationClient.setLocOption(option); locationClient.registerLocationListener(new BDLocationListener() { @Override public void onReceiveLocation(BDLocation result) { if (result != null) { MyLocationData data = new MyLocationData.Builder() .latitude(result.getLatitude()) .longitude(result.getLongitude()).build(); baiduMap.setMyLocationData(data); } } }); geo = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo); MyLocationConfiguration configuration = new MyLocationConfiguration( MyLocationConfiguration.LocationMode.FOLLOWING, true, geo); baiduMap.setMyLocationConfigeration(configuration);// 设置定位模式 baiduMap.setMyLocationEnabled(true);// 打开定位图层 } @Override protected void onStart() { locationClient.start(); super.onStart(); } @Override protected void onPause() { locationClient.stop(); super.onPause(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_1: //正常 baiduMap.setMyLocationConfigeration(new MyLocationConfiguration( MyLocationConfiguration.LocationMode.NORMAL, true, geo)); break; case KeyEvent.KEYCODE_2: //罗盘 baiduMap.setMyLocationConfigeration(new MyLocationConfiguration( MyLocationConfiguration.LocationMode.COMPASS, true, geo)); break; case KeyEvent.KEYCODE_3: //跟随 baiduMap.setMyLocationConfigeration(new MyLocationConfiguration( MyLocationConfiguration.LocationMode.FOLLOWING, true, geo)); break; default: break; } return super.onKeyDown(keyCode, event); } }
离线文件下载
http://shouji.baidu.com/map/map.html?from=3052,指定机型和城市下载对应的离线地图/** 离线地图初始化 **/ mOffline = new MKOfflineMap(); mOffline.init(mBMapMan, new MKOfflineMapListener() { public void onGetOfflineMapState(int type, int state) { switch (type) { case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: { MKOLUpdateElement update = mOffline.getUpdateInfo(state); //mText.setText(String.format("%s : %d%%", update.cityName, update.ratio)); } break; case MKOfflineMap.TYPE_NEW_OFFLINE: Log.d("OfflineDemo", String.format("add offlinemapnum:%d", state)); break; case MKOfflineMap.TYPE_VER_UPDATE: Log.d("OfflineDemo", String.format("new offlinemapver")); break; } } } ); /** 离线地图导入离线包 **/ intnum = mOffline.scan();