百度地图SDK的三个核心类
SDKInitializer
MapView
BaiduMap
一.环境搭建
获取API Key
http://developer.baidu.com/map/android-mobile-apply-key.htm
在获取key时要添加包名和sha1 , sha1值 通过下面方法获取,也可以看百度官方文档
1、打开命令行输入cd .android 进入到android签名目录 如:C:\Users\lenovo\.android
2、输入 keytool -list -v -keystore debug.keystore 密码是android
从SDK中官方给的例子Demo中的libs中复制出需要的jar包和.so文件到项目中
清单文件添加权限
<!-- 这个权限用于进行网络定位-->
<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>
二. 基础功能 (展示 缩放 平移 旋转 底图交通图卫星图层的展示)
首先初始化SDK
再加载布局
初始化SDK时要通过创建广播接受者校验下是否初始化成功
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//1 初始化sdk
initSDK();
setContentView(R.layout.activity_main);
}
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
private void initSDK() {
SDKInitializer.initialize(getApplicationContext());
IntentFilter filter = new IntentFilter();
filter.addAction(SDKInitializer.SDK_BROADCAST_ACTION_STRING_NETWORK_ERROR);//网络错误
filter.addAction(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR);//权限错误 即key有问题
BaiduMapReceiver receiver = new BaiduMapReceiver();
//2 注册广播校验key失败和成功
registerReceiver(receiver , filter );
}
class BaiduMapReceiver 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(context, "网络错误", Toast.LENGTH_SHORT).show();
}else if(action.equals(SDKInitializer.SDK_BROADTCAST_ACTION_STRING_PERMISSION_CHECK_ERROR)){
Toast.makeText(context, "校验错误", Toast.LENGTH_SHORT).show();
}
}
}
在布局文件中设置mapView控件
<!-- 添加百度地图控件 -->
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
在oncreate()方法中初始化化控件
设置默认缩放级别 默认显示位置 默认是否显示标尺和缩放按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.......
intiView();
}
private void intiView() {
mMapView = (MapView) findViewById(R.id.bmapView);
//设置显示<span style="font-family: Arial, Helvetica, sans-serif;">缩放控件</span>
// mMapView.showScaleControl(false);
//设置显示标尺控件
// mMapView.showZoomControls(false);
baiduMap = mMapView.getMap();//获取地图管理者
//设置缩放级别
baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15));//默认级别是12
//设置打开地图显示的位置 可以百度地图开发者中心 拾取想要的坐标点
baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(39.999452, 116.402131)));//鸟巢 116.402131,39.999452
}
设置按键缩放 平移 旋转
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_0:
//一级一级的放大
baiduMap.setMapStatus(MapStatusUpdateFactory.zoomIn());
break;
case KeyEvent.KEYCODE_1:
//一级一级的缩小
baiduMap.setMapStatus(MapStatusUpdateFactory.zoomOut());
break;
case KeyEvent.KEYCODE_2:
//以地图中心点旋转
float rotate = baiduMap.getMapStatus().rotate;//获取当前角度
MapStatus newRotate = new MapStatus.Builder().rotate(rotate + 30).build();//设置新的角度
baiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(newRotate));
break;
case KeyEvent.KEYCODE_3:
//俯角旋转
float overlook = baiduMap.getMapStatus().overlook;//获取当前俯角
MapStatus newOverlook = new MapStatus.Builder().overlook(overlook - 15).build();
baiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(newOverlook));
break;
case KeyEvent.KEYCODE_4:
//移动到某一个点
baiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(new LatLng(39.999452, 116.402131)));
break;
}
return super.onKeyDown(keyCode, event);
}
一定要记得重写activity的几个生命周期
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}
底图 交通图 卫星图的切换
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_1:
// 显示底图
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
baiduMap.setTrafficEnabled(false);
break;
case KeyEvent.KEYCODE_2:
// 显示交通图
baiduMap.setTrafficEnabled(true);
break;
case KeyEvent.KEYCODE_3:
// 显示卫星图
baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
baiduMap.setTrafficEnabled(false);
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
三. 覆盖物
覆盖物包括:本地覆盖物和搜索覆盖物
本地覆盖物的抽象基类:OverlayOptions(核心类)
圆形覆盖物: CircleOptions
文字覆盖物: TextOptions
marker覆盖物: MarkerOptions
圆点覆盖物:DotOptions
ground 覆盖物:GroundOverlayOptions
圆点覆盖物:DotOptions
多边形覆盖物:PolygonOptions
折线覆盖物:PolylineOptions
弧线覆盖物:ArcOptions
圆形覆盖物
<span style="white-space:pre"> </span>protected double latitude = 40.110234;
protected double longitude = 116.373475;
protected LatLng latlng = new LatLng(latitude, longitude);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawCircle();//绘制圆形覆盖物
}
private void drawCircle() {
// 绘制圆形覆盖物
// 1、创建覆盖物对象
CircleOptions circleOptions = new CircleOptions();
// 2、设置数据
circleOptions.center(latlng);// 设置中心点
circleOptions.fillColor(0x33ff0000);// 设置填充颜色
circleOptions.radius(1000);// 设置半径 单位是米
circleOptions.stroke(new Stroke(5, 0xff00ff00));
// 3、把覆盖物添加到地图上
baiduMap.addOverlay(circleOptions);
}
文字覆盖物
<span style="white-space:pre"> </span>@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawText();//绘制文字覆盖物
}
private void drawText() {
TextOptions textOptions = new TextOptions();
textOptions.position(latlng);// 设置位置
textOptions.bgColor(0x33ff0000);// 设置背景颜色
textOptions.fontColor(0xff00ff00);// 设置文字颜色
textOptions.fontSize(30);// 设置文字大小
textOptions.typeface(Typeface.SERIF);// 设置字体
textOptions.text("哈哈哈哈");
baiduMap.addOverlay(textOptions);
}
marker覆盖物
可以放图片 可设置拖动 可设置点击事件
public class MarkerOptionsDemo extends BaseActivity implements OnMarkerClickListener {
private View pop;
private TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initPop();
draw();
}
//点击mark覆盖物时弹出的popwindow
private void initPop() {
pop = View.inflate(this, R.layout.pop, null);
title = (TextView) pop.findViewById(R.id.title);
pop.setVisibility(View.INVISIBLE);
MapViewLayoutParams param = new MapViewLayoutParams.Builder()
.layoutMode(ELayoutMode.mapMode)// 使用经纬度模式
.position(latlng)// 设置控件跟着地图移动
.width(MapViewLayoutParams.WRAP_CONTENT)
.height(MapViewLayoutParams.WRAP_CONTENT)
.build();
mapview.addView(pop, param);
}
private void draw() {
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.drawable.eat_icon);
MarkerOptions option = new MarkerOptions();
option.position(latlng).icon(bitmap).title("程序猿");
option.draggable(true);// 设置可以拖拽
baiduMap.addOverlay(option);
option = new MarkerOptions().title("向北")
.position(new LatLng(latitude + 0.001, longitude)).icon(bitmap);
baiduMap.addOverlay(option);
option = new MarkerOptions().title("向东")
.position(new LatLng(latitude, longitude + 0.001)).icon(bitmap);
baiduMap.addOverlay(option);
ArrayList<BitmapDescriptor> bitmaps = new ArrayList<BitmapDescriptor>();
bitmaps.add(bitmap);
bitmaps.add(BitmapDescriptorFactory
.fromResource(R.drawable.icon_geo));
option = new MarkerOptions().title("向西南")
.position(new LatLng(latitude - 0.001, longitude - 0.001))
.icon(bitmap);
// .icons(bitmaps);
baiduMap.addOverlay(option);
// 把事件传递给Marker覆盖物
baiduMap.setOnMarkerClickListener(this);
}
@Override
public boolean onMarkerClick(Marker marker) {
// Toast.makeText(this, marker.getTitle(), 0).show();
// 显示pop,更新经纬度,更新标题
pop.setVisibility(View.VISIBLE);
MapViewLayoutParams param = new MapViewLayoutParams.Builder()
.layoutMode(ELayoutMode.mapMode)// 使用经纬度模式
.position(marker.getPosition())// 设置控件跟着地图移动
.width(MapViewLayoutParams.WRAP_CONTENT)
.height(MapViewLayoutParams.WRAP_CONTENT)
.yOffset(-10)
.build();
mapview.updateViewLayout(pop, param);
title.setText(marker.getTitle());
return true;
}
}
范围搜索
public class SearchInBoundDemo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
search();
}
private void search() {
// 初始化搜索对象
PoiSearch poiSearch = PoiSearch.newInstance();
//注册搜索结果监听
poiSearch.setOnGetPoiSearchResultListener(new MyOnGetPoiSearchResultListener());
PoiBoundSearchOption option = new PoiBoundSearchOption();
// 设置矩形范围
LatLngBounds bounds = new LatLngBounds.Builder()
// 116.387414,40.117419
.include(new LatLng(40.117419, 116.387414))
.include(new LatLng(40.101527, 116.35838)).build();
option.keyword("公交");
option.bound(bounds);
poiSearch.searchInBound(option);//在指定范围搜索指定内容
}
class MyOnGetPoiSearchResultListener implements OnGetPoiSearchResultListener{
@Override
public void onGetPoiDetailResult(PoiDetailResult arg0) {
}
@Override
public void onGetPoiResult(PoiResult result) {
if(result==null||result.error==SearchResult.ERRORNO.RESULT_NOT_FOUND){
Toast.makeText(getApplicationContext(), "没有结果", 0).show();
return;
}
// 把搜索到的结果,显示到地图上
// result.getAllPoi()
PoiOverlay poiOverlay = new MyPoiOverlay(baiduMap);
// 把事件传递给覆盖物
baiduMap.setOnMarkerClickListener(poiOverlay);
// 设置数据
poiOverlay.setData(result);
// 把覆盖物添加到地图上
poiOverlay.addToMap();
}
}
class MyPoiOverlay extends PoiOverlay{
public MyPoiOverlay(BaiduMap arg0) {
super(arg0);
}
@Override
public boolean onPoiClick(int index) {
// 得到当前位置对应的兴趣点信息
PoiInfo poiInfo = getPoiResult().getAllPoi().get(index);
Toast.makeText(getApplicationContext(), poiInfo.name, 0).show();
return super.onPoiClick(index);
}
}
}
周边搜索
public class SearchNearbyDemo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
search();
}
private void search() {
// 初始化搜索对象
PoiSearch poiSearch = PoiSearch.newInstance();
poiSearch.setOnGetPoiSearchResultListener(new MyOnGetPoiSearchResultListener());
PoiNearbySearchOption option = new PoiNearbySearchOption();
// 设置中心点
option.location(latlng);
// 设置半径
option.radius(1000);
// 设置关键字
option.keyword("公交");
poiSearch.searchNearby(option);
}
class MyOnGetPoiSearchResultListener implements OnGetPoiSearchResultListener{
@Override
public void onGetPoiDetailResult(PoiDetailResult arg0) {
}
@Override
public void onGetPoiResult(PoiResult result) {
if(result==null||result.error==SearchResult.ERRORNO.RESULT_NOT_FOUND){
Toast.makeText(getApplicationContext(), "没有结果", 0).show();
return;
}
// 把搜索到的结果,显示到地图上
// result.getAllPoi()
PoiOverlay poiOverlay = new MyPoiOverlay(baiduMap);
// 把事件传递给覆盖物
baiduMap.setOnMarkerClickListener(poiOverlay);
// 设置数据
poiOverlay.setData(result);
// 把覆盖物添加到地图上
poiOverlay.addToMap();
}
}
class MyPoiOverlay extends PoiOverlay{
public MyPoiOverlay(BaiduMap arg0) {
super(arg0);
}
@Override
public boolean onPoiClick(int index) {
// 得到当前位置对应的兴趣点信息
PoiInfo poiInfo = getPoiResult().getAllPoi().get(index);
Toast.makeText(getApplicationContext(), poiInfo.name, 0).show();
return super.onPoiClick(index);
}
}
}
城市搜索
public class SearchInCityDemo extends BaseActivity {
private PoiSearch poiSearch;
private int index;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 初始化搜索对象
poiSearch = PoiSearch.newInstance();
poiSearch
.setOnGetPoiSearchResultListener(new MyOnGetPoiSearchResultListener());
search();
}
private void search() {
PoiCitySearchOption option = new PoiCitySearchOption();
option.city("北京");
option.keyword("公交");
option.pageNum(index);
poiSearch.searchInCity(option);
}
@Override
public void onBackPressed() {
index++;
search();
}
class MyOnGetPoiSearchResultListener implements
OnGetPoiSearchResultListener {
@Override
public void onGetPoiDetailResult(PoiDetailResult result) {
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(getApplicationContext(), "没有结果", 0).show();
return;
}
Toast.makeText(getApplicationContext(), result.getAddress(), 0).show();
}
@Override
public void onGetPoiResult(PoiResult result) {
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(getApplicationContext(), "没有结果", 0).show();
return;
}
String text = "共" + result.getTotalPoiNum() + "条,共"
+ result.getTotalPageNum() + "页,当前第"
+ result.getCurrentPageNum() + "页,当前页"
+ result.getCurrentPageCapacity() + "条";
Toast.makeText(getApplicationContext(), text, 0).show();
// 把搜索到的结果,显示到地图上
// result.getAllPoi()
// 清除之前的结果
baiduMap.clear();
PoiOverlay poiOverlay = new MyPoiOverlay(baiduMap);
// 把事件传递给覆盖物
baiduMap.setOnMarkerClickListener(poiOverlay);
// 设置数据
poiOverlay.setData(result);
// 把覆盖物添加到地图上
poiOverlay.addToMap();
poiOverlay.zoomToSpan();// 缩放地图,使所有Overlay都在合适的视野内
}
}
class MyPoiOverlay extends PoiOverlay {
public MyPoiOverlay(BaiduMap arg0) {
super(arg0);
}
@Override
public boolean onPoiClick(int index) {
// 得到当前位置对应的兴趣点信息
PoiInfo poiInfo = getPoiResult().getAllPoi().get(index);
// Toast.makeText(getApplicationContext(), poiInfo.name, 0).show();
// 发起详情搜索
PoiDetailSearchOption option = new PoiDetailSearchOption();
option.poiUid(poiInfo.uid);
poiSearch.searchPoiDetail(option);
return super.onPoiClick(index);
}
}
}
五. 路线查询
步行路线
public class WalkingSearchDemo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
search();
}
private void search() {
RoutePlanSearch planSearch = RoutePlanSearch.newInstance();
planSearch.setOnGetRoutePlanResultListener(new MyOnGetRoutePlanResultListener());
WalkingRoutePlanOption option = new WalkingRoutePlanOption();
PlanNode start = PlanNode.withLocation(latlng);
option.from(start);
PlanNode to = PlanNode.withLocation(new LatLng(40.119516,116.383245));
option.to(to);
planSearch.walkingSearch(option);
}
class MyOnGetRoutePlanResultListener implements OnGetRoutePlanResultListener{
@Override
public void onGetDrivingRouteResult(DrivingRouteResult result) {
}
@Override
public void onGetTransitRouteResult(TransitRouteResult arg0) {
}
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(getApplicationContext(), "没有结果", 0).show();
return;
}
WalkingRouteOverlay routeOverlay = new WalkingRouteOverlay(baiduMap);
baiduMap.setOnMarkerClickListener(routeOverlay);
routeOverlay.setData(result.getRouteLines().get(0));
routeOverlay.addToMap();
routeOverlay.zoomToSpan();
}
}
}
驾车路线
public class DrivingSearchDemo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
search();
}
private void search() {
RoutePlanSearch planSearch = RoutePlanSearch.newInstance();
planSearch.setOnGetRoutePlanResultListener(new MyOnGetRoutePlanResultListener());
DrivingRoutePlanOption option = new DrivingRoutePlanOption();
// 设置策略 时间最短 费用最少 最短局距离等
option.policy(DrivingRoutePlanOption.DrivingPolicy.ECAR_TIME_FIRST);
PlanNode start = PlanNode.withLocation(latlng);
option.from(start);//起点
PlanNode to = PlanNode.withCityNameAndPlaceName("北京", "天安门");//终点
option.to(to);
//可以设置路过哪些地方
List<PlanNode> nodes = new ArrayList<PlanNode>();
nodes.add(PlanNode.withLocation(new LatLng(39.942651,116.459228)));
option.passBy(nodes);
planSearch.drivingSearch(option);
}
class MyOnGetRoutePlanResultListener implements OnGetRoutePlanResultListener{
@Override
public void onGetDrivingRouteResult(DrivingRouteResult result) {
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(getApplicationContext(), "没有结果", 0).show();
return;
}
DrivingRouteOverlay routeOverlay = new MyDrivingRouteOverlay(baiduMap);
baiduMap.setOnMarkerClickListener(routeOverlay);
routeOverlay.setData(result.getRouteLines().get(0));
routeOverlay.addToMap();
routeOverlay.zoomToSpan();
}
@Override
public void onGetTransitRouteResult(TransitRouteResult arg0) {
}
@Override
public void onGetWalkingRouteResult(WalkingRouteResult arg0) {
}
}
class MyDrivingRouteOverlay extends DrivingRouteOverlay{
public MyDrivingRouteOverlay(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);
}
}
}
公交换乘
public class TransitSearchDemo extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
search();
}
private void search() {
RoutePlanSearch planSearch = RoutePlanSearch.newInstance();
planSearch.setOnGetRoutePlanResultListener(new MyOnGetRoutePlanResultListener());
TransitRoutePlanOption option = new TransitRoutePlanOption();
option.policy(TransitPolicy.EBUS_WALK_FIRST);//少走路策略
PlanNode to = PlanNode.withLocation(new LatLng(39.942651,116.459228));
option.from(PlanNode.withLocation(latlng));
option.to(to);
option.city("北京");
planSearch.transitSearch(option);
}
class MyOnGetRoutePlanResultListener implements OnGetRoutePlanResultListener{
@Override
public void onGetDrivingRouteResult(DrivingRouteResult result) {
}
@Override
public void onGetTransitRouteResult(TransitRouteResult result) {
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(getApplicationContext(), "没有结果", 0).show();
return;
}
TransitRouteOverlay routeOverlay = new TransitRouteOverlay(baiduMap);
baiduMap.setOnMarkerClickListener(routeOverlay);
routeOverlay.setData(result.getRouteLines().get(0));
routeOverlay.addToMap();
routeOverlay.zoomToSpan();
}
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {}
}
}
六.其它内容检索
地址解析服务
GeoCoder.geocode(GeoCodeOption option)
根据地址名获取地址信息 异步函数,返回结果在OnGetGeoCoderResultListener里的onGetGeoCodeResult方法通知
GeoCoder.reverseGeoCode(ReverseGeoCodeOption option)
根据地理坐标点获取地址信息 异步函数,返回结果在OnGetGeoCoderResultListener里的onGetReverseGeoCodeResult方法通知
联想词检索
SuggestionSearch . requestSuggestion(SuggestionSearchOption option)
查询一系列与指定key相关的内容,结果中包括城市及包含key的名称
结果处理OnGetSuggestionResultListener . onGetSuggestionResult(SuggestionResult result)
公交路线详细信息搜索
检索: BusLineSearch. searchBusLine(BusLineSearchOption option)
busLineUid信息获取:公交线路的uid,可以通过poi查询返回的结果中获取MKPoiInfo的uid。
使用poiSearchInCity查询公交线信息,利用PoiInfo的type可以判断poi类型,当类型为公交线路时,记录当前的PoiInfo中的uid信息。
利用获取的uid信息进行公交线的查询
结果处理:在OnGetBusLineSearchResultListener. onGetBusLineResult(BusLineResult result)中进行结果的处理,此时使用到的覆盖物是BusLineOverlay
七.定位
把定位需要的.so文件和jar放到libs目录下
在清单文件中配置个服务 并且要设置为独立的进程 这样在后台也可定位
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
activity中
public class LocationDemo extends BaseActivity {
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyListener();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locate();
}
private void locate() {
mLocationClient = new LocationClient(this);
mLocationClient.registerLocationListener(myListener);//注册定位监听
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系
int span = 1000;
option.setScanSpan(span);// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);// 可选,默认false,设置是否使用gps
option.setLocationNotify(true);// 可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
option.setIgnoreKillProcess(false);// 可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);// 可选,默认false,设置是否收集CRASH信息,默认收集
mLocationClient.setLocOption(option);
// 设置定位的显示模式
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.COMPASS, true, icon);
baiduMap.setMyLocationConfigeration(config);
}
@Override
protected void onStart() {
super.onStart();
// 开始定位
mLocationClient.start();
}
@Override
protected void onPause() {
super.onPause();
// 停止定位
mLocationClient.stop();
}
class MyListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation result) {
// 显示定位结果
baiduMap.setMyLocationEnabled(true);
MyLocationData data = new MyLocationData.Builder()
.latitude(result.getLatitude())
.longitude(result.getLongitude())
.build();
baiduMap.setMyLocationData(data);//设置定位数据, 只有先允许定位图层后设置数据才会生效,参见 setMyLocationEnabled(boolean)
}
}
}