地图定位 绘制公交线路




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();
    }

[源代码打包下载]



转自:http://yuncode.net/code/c_5170af443a3e925
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用百度地图API绘制线路及线路名称,您可以按照以下步骤进行操作: 1. 引入百度地图API: ```html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"></script> ``` 其中,密钥需要您根据自己的百度地图开发者账号生成。 2. 创建地图容器: ```html <div id="map" style="width:100%;height:500px;"></div> ``` 3. 初始化地图: ```javascript var map = new BMap.Map("map"); // 创建地图实例 var point = new BMap.Point(116.404, 39.915); // 创建点坐标 map.centerAndZoom(point, 15); // 初始化地图,设置中心点和缩放级别 ``` 4. 绘制线路: ```javascript var polyline = new BMap.Polyline([ new BMap.Point(116.399, 39.910), new BMap.Point(116.405, 39.920), new BMap.Point(116.415, 39.930) ], {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5}); // 创建折线对象 map.addOverlay(polyline); // 添加折线到地图上 ``` 其中,`BMap.Polyline`接受一个点数组参数,表示线路经过的点坐标,同时还可以设置线路样式等属性。 5. 添加线路名称标注: ```javascript var label = new BMap.Label("线路名称", {offset:new BMap.Size(20,-10)}); // 创建标注对象 label.setStyle({border:"none", backgroundColor:"transparent", color:"red", fontSize:"16px"}); // 设置标注样式 polyline.getLabel = function(){ // 给折线对象添加getLabel方法 return label; } map.addOverlay(label); // 添加标注到地图上 ``` 其中,`BMap.Label`接受一个字符串参数,表示标注文本内容,同时还可以设置标注样式等属性。需要注意的是,将标注对象添加到折线对象上可以保证标注随着折线一起移动。 以上就是使用百度地图API绘制线路及线路名称的基本步骤,您可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值