一、问题描述

基于百度地图实现检索指定城市指定公交的交通路线图,效果如图所示

二、通用组件Application类,主要创建并初始化BMapManager
    public class App extends Application {
static App mDemoApp;
//百度MapAPI的管理类
public BMapManager mBMapMan = null;
// 授权Key
// 申请地址:http://dev.baidu.com/wiki/static/imap/key/
public String mStrKey = "Your APPKey";
boolean m_bKeyRight = true; // 授权Key正确,验证通过
// 常用事件监听,用来处理通常的网络错误,授权验证错误等
public static class MyGeneralListener implements MKGeneralListener {
@Override
public void onGetNetworkState(int iError) {
Log.d("MyGeneralListener", "onGetNetworkState error is "+ iError);
Toast.makeText(App.mDemoApp.getApplicationContext(), "您的网络出错啦!",
Toast.LENGTH_LONG).show();
}
@Override
public void onGetPermissionState(int iError) {
Log.d("MyGeneralListener", "onGetPermissionState error is "+ iError);
if (iError == MKEvent.ERROR_PERMISSION_DENIED) {
// 授权Key错误:
Toast.makeText(App.mDemoApp.getApplicationContext(),
"文件输入正确的授权Key!",
Toast.LENGTH_LONG).show();
App.mDemoApp.m_bKeyRight = false;
}
}
}
@Override
public void onCreate() {
Log.v("BMapApiDemoApp", "onCreate");
mDemoApp = this;
mBMapMan = new BMapManager(this);
mBMapMan.init(this.mStrKey, new MyGeneralListener());
mBMapMan.getLocationManager().setNotifyInternal(10, 5); super.onCreate();
} @Override
//app的退出之前调用mapadpi的destroy()函数,避免重复初始化带来的时间消耗
public void onTerminate() {
if (mBMapMan != null) {
mBMapMan.destroy();
mBMapMan = null;
}
super.onTerminate();
} }
三、编写公交的路线图层(CustomRouteOverLay)和图标标识(CustomOverlayItem)

  CustomRouteOverLay组件扩展RouteOverlay:

主要公交、步行和驾车线路图层,将公交、步行和驾车出行方案的路线及关键点显示在地图上,根据车辆路线的起点和终点进行驾车路线的检索;

  CustomOverlayItem扩展ItemizedOverlay<OverlayItem>:

覆盖物的集合类,使用这个类可以将地图上具有相同属性或者特性的坐标使用图标标识出来,OverLayItem 这个类对象则是ItemizedOverLay中一个一个的Item对象 也就是每个坐标对应的覆盖物

  CustomRouteOverLay类代码:

public class CustomRouteOverLay extends RouteOverlay {
public Activity ac;
private MapView mapView;
static ArrayList<View> overlayviews = new ArrayList<View>();
public CustomRouteOverLay(Activity arg0, MapView arg1) {
super(arg0, arg1);
ac = arg0;
mapView = arg1;
// TODO Auto-generated constructor stub
} @Override
protected boolean onTap(int arg0) {
// TODO Auto-generated method stub
// return super.onTap(arg0);
return true;
} @Override
public void setData(MKRoute arg0) {
// TODO Auto-generated method stub
super.setData(arg0);
addHint(arg0);
} public void addHints(MKRoute routes) {
for (int i = 0; i < routes.getNumSteps(); i++) {
Drawable marker = ac.getResources().getDrawable(R.drawable.pop); // 得到需要标在地图上的资源
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight()); // 为maker定义位置和边界
OverItemT overitem = new OverItemT(marker,ac, routes.getStep(i).getContent(),routes.getStep(i).getPoint());
// OverlayItem over=new OverlayItem(routes.GET, null, null);
mapView.getOverlays().add(overitem); // 添加ItemizedOverlay实例到mMapView
}
mapView.invalidate();
}
/**
* 增加 指示路线
* @param routes
*/
public void addHint(MKRoute routes) {
mapView.getOverlays().clear();// 先清空
// mapView.removeAllViewsInLayout();
View mPopView = ac.getLayoutInflater().inflate(R.layout.popview,
null);
for(int i=0;i< overlayviews.size();i++){
System.out.println("remove &"+i);
mapView.removeViewInLayout(overlayviews.get(i));
overlayviews.remove(i);
} mapView.invalidate();
// 添加ItemizedOverlay
for (int i = 0; i < routes.getNumSteps(); i++) { Drawable marker = ac.getResources().getDrawable(R.drawable.pop); // 得到需要标在地图上的资源
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight()); // 为maker定义位置和边界
GeoPoint pt = routes.getStep(i).getPoint();// =
// routes.get(i).getPoint();
if (i != 0 && i != routes.getNumSteps() - 1) {
mPopView = ac.getLayoutInflater().inflate(R.layout.popview,
null);
mapView.addView(mPopView, new MapView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,
MapView.LayoutParams.TOP_LEFT));
mPopView.setVisibility(View.GONE);
mapView.updateViewLayout(mPopView, new MapView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, pt,
MapView.LayoutParams.BOTTOM_CENTER));
mPopView.setVisibility(View.VISIBLE);
Button button = (Button) mPopView.findViewById(R.id.overlay_pop);
button.setText(routes.getStep(i).getContent());
overlayviews.add(mPopView);
overlayviews.add(button);
} else {
//修改起始点和终点样式-自定义
mPopView = ac.getLayoutInflater().inflate(R.layout.popview,
null);
mapView.addView(mPopView, new MapView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,
MapView.LayoutParams.TOP_LEFT));
mPopView.setVisibility(View.GONE);
mapView.updateViewLayout(mPopView, new MapView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, pt,
MapView.LayoutParams.BOTTOM_CENTER));
mPopView.setVisibility(View.VISIBLE);
Button button = (Button) mPopView.findViewById(R.id.overlay_pop);
button.offsetTopAndBottom(100);
button.setTextColor(Color.BLUE);
button.setBackgroundColor(Color.TRANSPARENT);
button.setText(routes.getStep(i).getContent());
overlayviews.add(mPopView);
overlayviews.add(button);
}
}
} class OverItemT extends ItemizedOverlay<OverlayItem> { private Drawable marker;
private Context mContext;
private GeoPoint p;
private OverlayItem o;
public OverItemT(Drawable marker, Context context, String title,GeoPoint p) {
super(boundCenterBottom(marker));
this.marker = marker;
this.mContext = context;
this.p = p;
// 构造OverlayItem的三个参数依次为:item的位置,标题文本,文字片段
o = new OverlayItem(p, title, title);
populate(); // createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法
}
public void updateOverlay() {
populate();
} @Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
Projection projection = mapView.getProjection();
for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList
OverlayItem overLayItem = getItem(index); // 得到给定索引的item
String title = overLayItem.getTitle();
// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
Point point = projection.toPixels(overLayItem.getPoint(), null);
// 可在此处添加您的绘制代码
Paint paintText = new Paint();
paintText.setColor(Color.BLUE);
paintText.setTextSize(15);
canvas.drawText(title, point.x - 30, point.y, paintText); // 绘制文本
}
super.draw(canvas, mapView, shadow);
// 调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素
boundCenterBottom(marker);
} @Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return o;
} @Override
public int size() {
// TODO Auto-generated method stub
return 1;
} @Override
// 处理当点击事件
protected boolean onTap(int i) {
// 更新气泡位置,并使之显示
return true;
} @Override
public boolean onTap(GeoPoint arg0, MapView arg1) {
// TODO Auto-generated method stub
// 消去弹出的气泡
// ItemizedOverlayDemo.mPopView.setVisibility(View.GONE);
return super.onTap(arg0, arg1);
}
} }

CustomOverlayItem代码:

public class CustomOverlayItem extends ItemizedOverlay<OverlayItem> {
// private List<OverlayItem> GeoList = new ArrayList<OverlayItem>();
private Context mContext;
private OverlayItem overlay;
boolean showtext;
// private String title;
private Drawable marker; public CustomOverlayItem(Drawable marker, Context context, GeoPoint p,
String title,String sinppet, boolean showtext) {
super(boundCenterBottom(marker));
this.mContext = context;
// 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
// point = p;
this.showtext = showtext;
// this.title = title;
this.marker = marker;
overlay = new OverlayItem(p, title, sinppet);
populate(); // createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法
} @Override
protected OverlayItem createItem(int i) {
return overlay;
} @Override
public int size() {
return 1;
} @Override
public void draw(Canvas canvas, MapView mapView, boolean arg2) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, arg2);
// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
Projection projection = mapView.getProjection();
String title = overlay.getTitle();
// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
Point point = projection.toPixels(overlay.getPoint(), null);
// 可在此处添加您的绘制代码
Paint paintText = new Paint();
Paint paint = new Paint();
paint.setAlpha(255);
paint.setColor(Color.DKGRAY);
paint.setStrokeWidth(5);
paintText.setColor(Color.BLUE);
paintText.setTextSize(15);
// canvas.drawCircle(point.x, point.y, 100, paint);
canvas.drawText(title, point.x-30, point.y-50, paintText); // 绘制文本
// 调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素
boundCenterBottom(marker);
} @Override
// 处理当点击事件
protected boolean onTap(int i) {
if (showtext)
Toast.makeText(this.mContext, overlay.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
}
四、编写主程序BuslineSearch,扩展MapActivity,实现地图信息的显示
public class BuslineSearch extends MapActivity {
Button mBtnSearch = null; // 搜索按钮
MapView mMapView = null; // 地图View
MKSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用
String mCityName = null;
LocationListener loc_listener;
App app = null;
static boolean flag = false;
static Thread thread;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buslinesearch); app = (App) this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(getApplication());
app.mBMapMan.init(app.mStrKey, new App.MyGeneralListener());
}
app.mBMapMan.start();
// 如果使用地图SDK,请初始化地图Activity
super.initMapActivity(app.mBMapMan);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.setBuiltInZoomControls(true);
// 设置在缩放动画过程中也显示overlay,默认为不绘制
mMapView.setDrawOverlayWhenZooming(true);
mMapView.setBuiltInZoomControls(true);
// 初始化搜索模块,注册事件监听
MapController mMapController = mMapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
(int) (116.404 * 1E6)); // 用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
mMapController.setCenter(point); // 设置地图中心点
mMapController.setZoom(15); // 设置地图zoom级别
mSearch = new MKSearch();
mSearch.init(app.mBMapMan, new MKSearchListener() {
public void onGetPoiResult(MKPoiResult res, int type, int error) {
// 错误号可参考MKEvent中的定义
if (error != 0 || res == null) {
Toast.makeText(BuslineSearch.this, "抱歉,未找到结果",
Toast.LENGTH_LONG).show();
return;
}
// System.out.println(res.toString());
// 找到公交路线poi node
MKPoiInfo curPoi = null;
int totalPoiNum = res.getNumPois();
for (int idx = 0; idx < totalPoiNum; idx++) {
Log.d("busline", "the busline is " + idx);
curPoi = res.getPoi(idx);
if (2 == curPoi.ePoiType) {
break;
}
}
mSearch.busLineSearch(mCityName, curPoi.uid);
} public void onGetDrivingRouteResult(MKDrivingRouteResult res,
int error) {
} public void onGetTransitRouteResult(MKTransitRouteResult res,
int error) {
res.getPlan(0).getDistance();
} public void onGetWalkingRouteResult(MKWalkingRouteResult res,
int error) {
} public void onGetAddrResult(MKAddrInfo res, int error) {
} public void onGetBusDetailResult(MKBusLineResult result, int iError) {
if (iError != 0 || result == null) {
Toast.makeText(BuslineSearch.this, "抱歉,未找到结果",
Toast.LENGTH_LONG).show();
return;
}
// result.getBusRoute().get
// result.getBusRoute().getStart().toString();
CustomRouteOverLay routeOverlay = new CustomRouteOverLay(
BuslineSearch.this, mMapView); routeOverlay.setData(result.getBusRoute());
mMapView.getOverlays().clear();
System.out.println(mMapView.getOverlays().size());
mMapView.getOverlays().add(routeOverlay);
mMapView.invalidate();
mMapView.getController().animateTo(
result.getBusRoute().getStart());
} @Override
public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
// TODO Auto-generated method stub }
}); // mLocationManager.requestLocationUpdates(listener);
// 注册定位事件
loc_listener = new LocationListener() { @Override
public void onLocationChanged(Location location) {
if (location != null) {
String strLog = String.format("您当前的位置:\r\n" + "纬度:%f\r\n"
+ "经度:%f", location.getLongitude(),
location.getLatitude());
flag = true;
Drawable marker = getResources()
.getDrawable(R.drawable.ic_launcher);
final GeoPoint p = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
CustomOverlayItem item = new CustomOverlayItem(marker,
BuslineSearch.this, p, "我的位置", "", false);
mMapView.getOverlays().add(item);
mMapView.getController().animateTo(p);
}
}
};
// 设定搜索按钮的响应
mBtnSearch = (Button) findViewById(R.id.search); OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
SearchButtonProcess(v);
}
}; mBtnSearch.setOnClickListener(clickListener);
} void SearchButtonProcess(View v) {
if (mBtnSearch.equals(v)) {
mMapView.getOverlays().clear();
mMapView.getOverlays().removeAll(mMapView.getOverlays());
mMapView.invalidate();
EditText editCity = (EditText) findViewById(R.id.city);
EditText editSearchKey = (EditText) findViewById(R.id.searchkey);
mCityName = editCity.getText().toString();
mSearch.poiSearchInCity(mCityName, editSearchKey.getText()
.toString());
}
} @Override
protected void onPause() {
if (null == app)
app = (App) this.getApplication();
app.mBMapMan.getLocationManager().removeUpdates(loc_listener);
app.mBMapMan.stop();
super.onPause();
} @Override
protected void onResume() {
if (null == app)
app = (App) this.getApplication();
app.mBMapMan.start();
super.onResume();
app.mBMapMan.getLocationManager().requestLocationUpdates(loc_listener);// 定位
} @Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
} @Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

技术咨询:
 

Android定位&地图&导航——自定义公交路线代码的更多相关文章

  1. Android定位&地图&导航——基于百度地图,实现自定义图标绘制并点击时弹出泡泡

    一.问题描述 上一次我们使用百度地图实现基本的定位功能,接下来我们继续实现搜索和定位,并使用LocationOverlay绘制定位位置,同时展示如何使用自定义图标绘制并点击时弹出泡泡 如图所示: 二. ...

  2. Android定位&地图&导航——基于百度地图实现的定位功能

    一.问题描述 LBS位置服务是android应用中重要的功能,应用越来越广泛,下面我们逐步学习和实现lbs相关的应用如定位.地图.导航等,首先我们看如何基于百度地图实现定位功能 二.配置环境 1.注册 ...

  3. Android定位&地图&导航——基于百度地图移动获取位置和自动定位

    一.问题描述 使用百度地图实现如图所示应用,首先自动定位当前我起始位置(小圆点位置),并跟随移动不断自动定位我的当前位置 百度Api不同版本使用会有些差异,本例中加入lib如下: 二.编写MyAppl ...

  4. Windows phone 8 学习笔记(8) 定位地图导航

    原文:Windows phone 8 学习笔记(8) 定位地图导航 Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模 ...

  5. Windows phone 8 学习笔记(8) 定位地图导航(转)

    Windows phone 8 已经不使用自家的bing地图,新地图控件可以指定制图模式.视图等.bing地图的定位误差比较大,在模拟器中测试新地图貌似比较理想.本节主要讲解下位置服务以及新地图控件的 ...

  6. Android 百度地图API 定位 导航

    看看这个利用百度地图定位并实现目的地导航的Demo. 首先看实现效果:                          进 入后首先会得到当前位置,在地图上显示出来.在输入框中输入目的地后,就会在地 ...

  7. android百度地图开发之自动定位所在位置与固定位置进行驾车,步行,公交路线搜索

    最近跟着百度地图API学地图开发,先是学了路径搜索,对于已知坐标的两点进行驾车.公交.步行三种路径的搜索(公交路径运行没效果,待学习中),后来又 学了定位功能,能够获取到自己所在位置的经纬度,但当将两 ...

  8. Android百度地图开发05之公交信息检索 + 路线规划

    在上一篇blog中介绍过POI检索的使用,本篇blog主要介绍公交信息检索和线路规划的内容. 公交信息检索 实际上,公交信息检索与POI检索.在线建议检索非常相似,也是把你需要检索的信息发送给百度地图 ...

  9. android笔记---百度地图api应用 (二) 获取公交路线的详细信息

    package com.example.bdtest; import com.baidu.mapapi.MKEvent; import com.baidu.mapapi.MKPlanNode; imp ...

随机推荐

  1. 编译器问题:运行maven,报错-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.

    1.新建环境变量M2_HOME 2.指向你的maven安装目录 例如 :M2_HOME=D:\Apps\apache-maven-3.3.9 3.进入Myeclipse进行修改,Window-> ...

  2. PYTHON第三天

    PYTHON之路 七.基本的if判断 最简单的流程处理: if ...else If简单练习: #!/usr/bin/env  python # -*-coding:utf-8 -*- #if 基本表 ...

  3. React与ES6(三)ES6类和方法绑定

    React与ES6系列: React与ES6(一)开篇介绍 React和ES6(二)ES6的类和ES7的property initializer React与ES6(三)ES6类和方法绑定 React ...

  4. MindFusion Pack for ASP.NET发布v2013.R2

    在MindFusion.Diagramming for WebForms中: 导入OpenOffice Draw文件 新的DrawImporter类允许你通过OpenOffice Draw Vecto ...

  5. 借助Process Explorer定位断电未保存的录音文件

    话说某大神(大婶)开会常偷懒,用Windows自带的录音机进行录音并用记事本记录会议精要却没有点击Ctrl+S的习惯,结果就给我找了今天的难题.(之前都是Office的自动保存在哪里……) 还是一样, ...

  6. 【Python自动化运维之路Day7】

    1. configparser模块 import configparser config = configparser.ConfigParser() #先把config应用一下configparser ...

  7. 使用jsPlumb制作流程图设计器

    jsPlumb是一个比较强大的绘图组件,它提供了一种方法,主要用于连接网页上的元素.在现代浏览器中,它使用SVG或者Canvas技术,而对于IE8以下(含IE8)的古董浏览器,则使用VML技术. 项目 ...

  8. WPF Dispatcher 一次小重构

    几个月之前因为项目需要,需要实现一个类似于WPF Dispatcher类的类,来实现一些线程的调度.之前因为一直做Asp.Net,根本没有钻到这个层次去,做的过程中,诸多不顺,重构了四五次,终于实现, ...

  9. FreeIconMaker - 在线创建免费和时尚的图标

    在设计一个网站或 Web 应用程序时,你不能否认网页设计工具的重要性,这些工具有助于简化您的任务和完成您的项目.FreeIconMaker.com 是一个免费的在线图标制作工具,您也可以创建自己的模板 ...

  10. com组件远程桌面rdp模块的调用

    rdp(remote desktop protocol)是一个多通道的协议,包括客户端视音传输.文件传输和通讯端口转向等等功能,通过压缩处理的数据网络传输也是相当快.我们在windows操作系统下面, ...