用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

地图定位 绘制公交线路

2013-04-19 作者: 云代码会员举报

[android]代码库

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);
				// 此处仅展示一个方案作为示例
				System.out.println(routeOverlay.size());
				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();
	}

[源代码打包下载]




网友评论    (发表评论)

共4 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...