zoukankan      html  css  js  c++  java
  • android 百度地图开发实例(转载)

    因为在我的寝室google基站定位返回的数据总是为空,所以换成百度地图,发现百度地图开发起来非常方便,提供了许多有用的工具,地图的加载速度也比google地图快许多。

    为了加强记忆,写一点android 百度地图开发常用的方法。

    1初始化

    MapManager mBMapMan = new BMapManager(this);
    boolean isSuccess = mBMapMan.init(this.mStrKey, new MyGeneralListener());

    isSuccess 的值为true,则地图初始化成功

    2定位|设置定位监听器

    mBMapMan.getLocationManager().setNotifyInternal(10, 5);
       mBMapMan.start();
       
       mLocationListener = new LocationListener(){

    public void onLocationChanged(Location location) 
    {
    // TODO Auto-generated method stub
    // TODO Auto-generated method stub
    if(location != null)
    {
    String strLog = String.format("您当前的位置: " +
    "纬度:%f " +
    "经度:%f",
    location.getLatitude(), location.getLongitude());
    Log.v("Application-strLog", strLog);
    mBMapMan.getLocationManager().removeUpdates( mLocationListener);
    }
    }



           };
           mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);

    3使用实例

      duduApp app = (duduApp)this.getApplication();
            // 如果使用地图SDK,请初始化地图Activity
            super.initMapActivity(app.mBMapMan);
            
            mMapView = (MapView)findViewById(R.id.map);
            mMapView.setBuiltInZoomControls(true);  //设置启用内置的缩放控件
            
            mMapController = mMapView.getController();  // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
            GeoPoint point = new GeoPoint(latitudeE6,longitudeE6);  //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
            mMapController.setCenter(point);  //设置地图中心点
            
            mMapController.setZoom(zoomLevel);    //设置地图zoom级别
            mMapView.setDrawOverlayWhenZooming(true);
            Drawable marker = getResources().getDrawable(R.drawable.iconmarka);  //得到需要标在地图上的资源
    marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
    .getIntrinsicHeight());   //为maker定义位置和边界

    OverItemT overitem = new OverItemT(marker,1);
    mMapView.getOverlays().add(overitem); //添加ItemizedO

    4覆盖物

    class OverItemT extends ItemizedOverlay<OverlayItem> {


    public List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
    private Drawable marker;





    public OverItemT(Drawable marker,int count) {
    super(boundCenterBottom(marker));


    this.marker = marker;


    // 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
    GeoPoint p1 = new GeoPoint(latitudeE6, longitudeE6);
    GeoPoint p2 = new GeoPoint((int)(DuDuBLL.getInstance(getApplicationContext()).model.bLatitude*1E6),
    (int)(DuDuBLL.getInstance(getApplicationContext()).model.bLongitude*1E6));

    // 构造OverlayItem的三个参数依次为:item的位置,标题文本,文字片段
    mGeoList.add(new OverlayItem(p1, "P1", "店家位置"));
    mGeoList.add(new OverlayItem(p2, "P2", "我的位置"));

    populate();  //createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法
    }


    public void updateOverlay()
    {
    populate();
    }


    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {


    // Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
    Projection projection = mapView.getProjection(); 
    Point p1=null;
    Point p2=null;
    for (int index = size() - 1; index >= 0; index--) 
    { // 遍历mGeoList
    OverlayItem overLayItem = getItem(index); // 得到给定索引的item

    String title = overLayItem.getTitle();

    Log.v("map_title", title);
    Paint paintText = new Paint();
    paintText.setColor(Color.BLUE);
    paintText.setTextSize(15);
    if(title.equals("P1"))
    {
    // 把经纬度变换到相对于MapView左上角的屏幕像素坐标
    p1= projection.toPixels(overLayItem.getPoint(), null); 

    // 可在此处添加您的绘制代码


    canvas.drawText("店家", p1.x-30, p1.y-20, paintText); // 绘制文本
    }
    else
    {
    p2= projection.toPixels(overLayItem.getPoint(), null); 
    canvas.drawText("我的位置", p2.x-30, p2.y-20,  paintText); // 绘制文本
    }
    }

    super.draw(canvas, mapView, shadow);
    //调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素
    boundCenterBottom(marker);
    }


    @Override
    protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mGeoList.get(i);
    }


    @Override
    public int size() {
    // TODO Auto-generated method stub
    return mGeoList.size();
    }
    @Override
    // 处理当点击事件
    protected boolean onTap(int i) {
    setFocus(mGeoList.get(i));

    return true;
    }


    @Override
    public boolean onTap(GeoPoint arg0, MapView arg1) {
    // TODO Auto-generated method stub
    // 消去弹出的气泡

    return super.onTap(arg0, arg1);
    }
    }
       
    }

    5路线查询

    MKSearch mSearch=new MKSearch();

    mSearch.init(app.mBMapMan,new MKSearchListener(){


    public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
    // TODO Auto-generated method stub

    }


    public void onGetBusDetailResult(MKBusLineResult arg0,
    int arg1) {
    // TODO Auto-generated method stub

    }


    public void onGetDrivingRouteResult(MKDrivingRouteResult res,int error) {
    // TODO Auto-generated method stub
    // TODO Auto-generated method stub
    // 错误号可参考MKEvent中的定义
    if (error != 0 || res == null) {
    Toast.makeText(mapActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
    return;
    }
             //获得路线距离
    int distance = res.getPlan(0).getRoute(0).getDistance();
    RouteOverlay routeOverlay = new RouteOverlay(mapActivity.this, mMapView);
    // 此处仅展示一个方案作为示例
    routeOverlay.setData(res.getPlan(0).getRoute(0));
     //  mMapView.getOverlays().clear();
    mMapView.getOverlays().add(routeOverlay);
    mMapView.invalidate();    
    mMapView.getController().animateTo(res.getStart().pt);
    }


    public void onGetPoiResult(MKPoiResult arg0, int arg1,
    int arg2) {
    // TODO Auto-generated method stub

    }


    public void onGetRGCShareUrlResult(String arg0, int arg1) {
    // TODO Auto-generated method stub

    }


    public void onGetSuggestionResult(MKSuggestionResult arg0,
    int arg1) {
    // TODO Auto-generated method stub

    }


    public void onGetTransitRouteResult(
    MKTransitRouteResult arg0, int arg1) {
    // TODO Auto-generated method stub

    }


    public void onGetWalkingRouteResult(
    MKWalkingRouteResult res, int error) {
    if (error != 0 || res == null) {
    Toast.makeText(mapActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
    return;
    }
             //获得路线距离
    int distance = res.getPlan(0).getRoute(0).getDistance();
    RouteOverlay routeOverlay = new RouteOverlay(mapActivity.this, mMapView);
    // 此处仅展示一个方案作为示例
    routeOverlay.setData(res.getPlan(0).getRoute(0));
       mMapView.getOverlays().clear();
    mMapView.getOverlays().add(routeOverlay);
    mMapView.invalidate();    
    mMapView.getController().animateTo(res.getStart().pt);

    }

    });

    //步行路线

    mSearch.walkingSearch(null, startNode , null, endNode);

    具体请参考http://developer.baidu.com/map/sdkandev-question.htm

    6google地图与百度地图坐标体系的装换

    转换方法使用的是百度的api。
    转换方法为:
    http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=120.0904441&y=30.3056719
    在type1和type2中填上相应的值就能转换。
    gps坐标的type=0
    google坐标的type=2
    baidu坐标的type=4
    得到的结果形式为:
    {“error”:0,”x”:”MTE2LjM1MTA3ODgzMDM=”,”y”:”MzkuOTgwOTIwNDEwMTU2″}x和y都经过了base64转换。

    百度地图开发起来非常方便,感兴趣的可以去看api

    http://developer.baidu.com/map/sdk-android.htm

  • 相关阅读:
    SVM – 线性分类器
    解决mybatis generator无法覆盖XML
    windows下IDEA的terminal配置bash命令
    mysqldump定时备份数据库
    linux清理日志脚本
    MySQL主从同步配置
    mysql binlog日志自动清理及手动删除
    linux搭建FTP服务器并整合Nginx
    mysql解除死锁状态
    git取消跟踪已版本控制的文件
  • 原文地址:https://www.cnblogs.com/airry66/p/4011626.html
Copyright © 2011-2022 走看看