1. 如何初始化百度地图
2. 开启百度地图的定位导航,共有两个方法
1)利用百度地图提供的MyLocationOverlay
2)利用百度地图提供的LocationListener进行监听我的位置的变化
3)百度画路线画路线需要继承实现回调接口
BMapManager mapManager = new BMapManager(getApplication());
//mStrKey为百度应用key
mapManager.init(mStrKey, null);
// 如果使用地图SDK,需要初始化地图Activity
super.initMapActivity(mapManager);
//开启百度地图API
mapManager.start();
//mapView为百度地图控件MapView
mapView.setBuiltInZoomControls(false);
mapView.setClickable(true);
mapView.setEnabled(true);
// 得到MapController实例,该实例可以对百度地图进行相关功能的设置,如设置百度地图的放大级别、定位等
mapController = mapView.getController();
//设置显示百度地图的缩放级别
mapController.setZoom(15);// 最大18级,(15)
1)利用百度地图提供的MyLocationOverlay
// 添加定位图层
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mapView);
// 注册GPS位置更新的事件,让地图能实时显示当前位置
myLocationOverlay.enableMyLocation();
// 开启磁场感应传感器
myLocationOverlay.enableCompass();
mapView.getOverlays().add(myLocationOverlay);
MKLocationManager locationManager = mapManager.getLocationManager();
locationManager.requestLocationUpdates(new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
updateMyLoading(location.getLatitude(), location.getLongitude());
}
}
});
private void updateMyLoading(double latitude, double longitude){
List o = mapView.getOverlays();
final GeoPoint pt = new GeoPoint((int) (location.getLatitude() * 1e6), (int) (location.getLongitude() * 1e6));
if (pt != null) {
o.remove(myOverItemT);
myOverItemT = getOverItemT(myLocation, pt);
o.add(myOverItemT);
mapView.invalidate();
}
}
public OverItemT getOverItemT(Drawable scenicIcon, GeoPoint geo){
//OverItemT该类我自个定义的,继承ItemizedOverlay,以来显示我的位置的点
OverItemT overLay = new OverItemT(scenicIcon, MapSearchActivity.this, geo, view, mapView);
return overLay;
}
public class MySearchListener implements MKSearchListener {
@Override
public void onGetAddrResult(MKAddrInfo result, int iError) {
}
@Override
public void onGetPoiResult(MKPoiResult result, int type, int iError) {
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {
// TODO Auto-generated method stub
if (result == null) {
return;
}
RouteOverlay routeOverlay = new RouteOverlay(MapSearchActivity.this, mapView);
// 此处仅展示一个方案作为示例
routeOverlay.setData(result.getPlan(0).getRoute(0));
mapView.getOverlays().add(routeOverlay);
}
}
发送请求
private boolean walkingSearch(GeoPoint startGeo, GeoPoint endGeo) {
// TODO Auto-generated method stub
if (startGeo == null || endGeo == null) {
return false;
}
//画步行路线
MKSearch mMKSearch = new MKSearch();
mMKSearch.init(mapManager, new MySearchListener());//注意,MKSearchListener只支持一个,以最后一次设置为准
MKPlanNode start = new MKPlanNode();
MKPlanNode end = new MKPlanNode();
start.pt = startGeo;
end.pt = endGeo;
// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
mMKSearch.walkingSearch(null, start, null, end);
return true;
}