百度地图应用

来源:互联网 发布:手机html编程工具 编辑:程序博客网 时间:2024/05/22 08:03
六大核心类
    MapView

            com.baidu.mapapi.map
            类 MapView
            java.lang.Object
              继承者 ViewGroup
                  继承者 com.baidu.mapapi.map.MapView
            public class MapView extends ViewGroup
            显示地图的View。
    一个显示地图的视图,当被焦点选中时,它能捕获按键事件和触摸手势去平移和缩放地图。

    MapController

            com.baidu.mapapi.map
            类 MapController
            java.lang.Object
              继承者 com.baidu.mapapi.map.MapController
           
              public class MapController extends Object
               地图控制器

    BMapManager

          java.lang.Object
          继承者 com.baidu.mapapi.BMapManager
        
        
          public class BMapManagerextends Object
        
          地图引擎管理类

    
    Overlay
          com.baidu.mapapi.map
          类 Overlay
         java.lang.Object
            继承者 com.baidu.platform.comapi.map.Overlay
            继承者 com.baidu.mapapi.map.Overlay
    
    直接已知子类: GraphicsOverlay, ItemizedOverlay, MyLocationOverlay, PoiOverlay, PopupOverlay, TextOverlay
    public abstract class Overlayextends Overlay
    
    Overlay是一个基类,它表示可以显示在地图上方的覆盖物。
    
    从2.0.0开始,SDK不支持直接继承Overlay , 用户可通过继承ItemizedOverlay来添加覆盖物。

    MKSearch

    java.lang.Object
      继承者 com.baidu.mapapi.search.MKSearch
    
    
    public class MKSearch extends Object
    
    搜索服务.
    
    用于位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索

    MKSearchListener
     接口 MKSearchListener

    public interface MKSearchListener
    
    搜索结果通知接口。
    

    该接口返回poi搜索,公交搜索,驾乘路线,步行路线结果

查询百度系统的坐标 http://api.map.baidu.com/lbsapi/getpoint/index.html

地图控制 (旋转,平移,缩放)

<span style="font-size:14px;">switch (keyCode) {case KeyEvent.KEYCODE_1:// 1 放大地图mapController.zoomIn();break;case KeyEvent.KEYCODE_2:// 2 缩小地图            mapController.zoomOut();break;case KeyEvent.KEYCODE_3:// 3 平面旋转(0到360)int mapRotation = mapView.getMapRotation();//获取当前mapview的平面角度System.out.println("mapRotation:" + mapRotation);mapController.setRotation(mapRotation + 30); break;case KeyEvent.KEYCODE_4:// 4俯视旋转(-45到0)int mapOverlooking = mapView.getMapOverlooking(); //获取当前mapview的俯视角度System.out.println("mapOverlooking:" + mapOverlooking);mapController.setOverlooking(mapOverlooking - 5);break;case KeyEvent.KEYCODE_5:// 5 平移//113.980861,22.540062int latitude = (int)22.540062E6;int longitudeE6 =  (int)113.980861E6;GeoPoint sjzc = new GeoPoint(latitude, longitudeE6);mapController.animateTo(sjzc);break;default:break;}</span>


文字覆盖

<span style="font-size:14px;">private void textOverlay() {// TODO Auto-generated method stub/* * 添加文字标注的一般流程:  * 1. 用TextItem准备文字数据 *  2. 创建TextOverlay,调用 addText(TextItem)接口添加文字.  * 3. 调用MapView.getOverlays().add()方法添加overlay到mapview中。  * 4. * 调用MapView.refresh() 使Overlay生效。 */// 1. 用TextItem准备文字数据TextItem textItem = new TextItem();/* * int align 文字对齐方式 ,为 ALIGN_TOP,ALIGN_CENTER, ALIGN_BOTTOM中的一个值 static * int ALIGN_BOTTOM 文字对齐参数,下边中点对齐 static int ALIGN_CENTER 文字对齐参数,中心对齐 * static int ALIGN_TOP 文字对齐参数,上边中点对齐 Symbol.Color bgColor 文字背景色, 默认为透明 * Symbol.Color fontColor 文字颜色 int fontSize 字号大小 GeoPoint pt * 文字显示的位置,用经纬度坐标表示 String text 要显示的文字内容 Typeface typeface 文字字体, android * 字体表示,为空则用系统默认字体. */textItem.align = TextItem.ALIGN_CENTER;Symbol.Color color = new Symbol().new Color();color.red = 255;textItem.fontColor = color;textItem.fontSize = 20;textItem.pt = centerszhm;//文字显示的坐标位置textItem.text = "黑马深圳中心";//2. 创建TextOverlay,调用 addText(TextItem)接口添加文字.TextOverlay textOverlay = new TextOverlay(mapView);textOverlay.addText(textItem);//3 调用MapView.getOverlays().add()方法添加overlay到mapview中mapView.getOverlays().add(textOverlay);//4 调用MapView.refresh() 使Overlay生效mapView.refresh();}</span>



地图图层

<span style="font-size:14px;">switch (keyCode) {case KeyEvent.KEYCODE_7:// 地图mapView.setTraffic(false);mapView.setSatellite(false);break;case KeyEvent.KEYCODE_8:// 卫星地图mapView.setTraffic(false);mapView.setSatellite(true);break;case KeyEvent.KEYCODE_9:// 交通地图mapView.setTraffic(true);mapView.setSatellite(false);break;default:break;}</span>


文字覆盖

<span style="font-size:14px;">private void textOverlay() {// TODO Auto-generated method stub/* * 添加文字标注的一般流程:  * 1. 用TextItem准备文字数据 *  2. 创建TextOverlay,调用 addText(TextItem)接口添加文字.  * 3. 调用MapView.getOverlays().add()方法添加overlay到mapview中。  * 4. * 调用MapView.refresh() 使Overlay生效。 */// 1. 用TextItem准备文字数据TextItem textItem = new TextItem();/* * int align 文字对齐方式 ,为 ALIGN_TOP,ALIGN_CENTER, ALIGN_BOTTOM中的一个值 static * int ALIGN_BOTTOM 文字对齐参数,下边中点对齐 static int ALIGN_CENTER 文字对齐参数,中心对齐 * static int ALIGN_TOP 文字对齐参数,上边中点对齐 Symbol.Color bgColor 文字背景色, 默认为透明 * Symbol.Color fontColor 文字颜色 int fontSize 字号大小 GeoPoint pt * 文字显示的位置,用经纬度坐标表示 String text 要显示的文字内容 Typeface typeface 文字字体, android * 字体表示,为空则用系统默认字体. */textItem.align = TextItem.ALIGN_CENTER;Symbol.Color color = new Symbol().new Color();color.red = 255;textItem.fontColor = color;textItem.fontSize = 20;textItem.pt = centerszhm;//文字显示的坐标位置textItem.text = "世界之窗";//2. 创建TextOverlay,调用 addText(TextItem)接口添加文字.TextOverlay textOverlay = new TextOverlay(mapView);textOverlay.addText(textItem);//3 调用MapView.getOverlays().add()方法添加overlay到mapview中mapView.getOverlays().add(textOverlay);//4 调用MapView.refresh() 使Overlay生效mapView.refresh();}</span>


图形覆盖

<span style="font-size:14px;">private void initGraphics() {//1,数据 GraphicGeometry g = new Geometry() ;/* * public void setCircle(GeoPoint geoPoint,                      int radius)设置图形为圆 参数:geoPoint - 地理坐标radius - 圆的半径,单位:米 */g.setCircle(centerszhm, 1000);Symbol s = new Symbol();/* * public void setSurface(Symbol.Color color,                       int status,                       int linewidth)设置面样式 参数:color - 颜色status - 填充状态,0表示不填充,1表示填充linewidth - 线宽,当填充状态为填充时线宽无意义 */Symbol.Color color = new Symbol().new Color();color.red = 255;color.alpha = 100;color.blue = 0;color.green = 0;s.setSurface(color, 1, 1);Graphic graphics = new Graphic(g, s) ;//2 给overlay设置数据GraphicsOverlay overlay = new GraphicsOverlay(mapView);overlay.setData(graphics);//3 把overlay加到mapView中mapView.getOverlays().add(overlay);//4,刷新mapView.refresh();}</span>

多条目覆盖

<span style="font-size:14px;">private void itemizedData() {/*添加覆盖物的一般流程: 1. 用OverlayItem 准备overlay数据。 2. 创建ItemizedOverlay实例,调用 addItem(OverlayItem) 接口添加overlay.  3. 若一次性添加的overlay数据较多,可以使用 addItem(List) 接口.  3. 调用MapView.getOverlays().add()方法添加overlay到mapview中。 4. 调用MapView.refresh() 使Overlay生效。*///1. 用OverlayItem 准备overlay数据。List<OverlayItem> items = new ArrayList<OverlayItem>();{GeoPoint center = new GeoPoint(latitude, longitudeE6);OverlayItem item = new OverlayItem(center,                "世界之窗A",                "满足愿望,找个好对象");items.add(item);}{GeoPoint center = new GeoPoint(latitude + 3000, longitudeE6 + 3000);OverlayItem item = new OverlayItem(center,                "</span><span style="font-size:14px;">世界之窗B</span><span style="font-size:14px;">",                "满足愿望,找个好对象");items.add(item);}{GeoPoint center = new GeoPoint(latitude + 3000, longitudeE6 - 3000);OverlayItem item = new OverlayItem(center,                "</span><span style="font-size:14px;">世界之窗C</span><span style="font-size:14px;">",                "满足愿望,找个好对象");items.add(item);}{GeoPoint center = new GeoPoint(latitude - 3000, longitudeE6 + 3000);OverlayItem item = new OverlayItem(center,                "</span><span style="font-size:14px;">世界之窗D</span><span style="font-size:14px;">",                "满足愿望,找个好对象");items.add(item);}{GeoPoint center = new GeoPoint(latitude - 3000, longitudeE6 - 3000);;OverlayItem item = new OverlayItem(center,                "</span><span style="font-size:14px;">世界之窗</span><span style="font-size:14px;">",                "满足愿望,找个好对象");items.add(item);}// 2. 创建ItemizedOverlay实例,调用 addItem(OverlayItem) 接口添加overlay. ItemizedOverlay overlay = new ItemizedOverlay(getResources().getDrawable(R.drawable.eat_icon),                mapView);overlay.addItem(items);//3. 调用MapView.getOverlays().add()方法添加overlay到mapview中mapView.getOverlays().add(overlay);//4.mapView.refresh();}</span>

半径检索数据

<span style="font-size:14px;">// 处理搜索结果        mksearch.init(mapManager, new MKSearchAdapter(){            @Override            public void onGetPoiResult(MKPoiResult result,  int type,                    int iError) {                                if (iError == 0 && result != null){//有搜索信息                    // 1,result.数据                    ArrayList<MKPoiInfo> allPoi = result.getAllPoi();                    // 2. overlay                    PoiOverlay overlay = new PoiOverlay(SearchNearByActivity.this, mapView);                    overlay.setData(allPoi);                    // 3. mapview add overlay                    mapView.getOverlays().add(overlay);                    // 4. flash                    mapView.refresh();                } else {                                    }                            }        });                //发起搜索        mksearch.poiSearchNearBy("电影院", centerszhm, 2000);</span>

分页显示poi数据
<span style="font-size:14px;">private void nearbySearch() {// 处理搜索结果mksearch.init(mapManager, new MKSearchAdapter(){@Overridepublic void onGetPoiResult(MKPoiResult result,  int type,                    int iError) {if (iError == 0 && result != null){//有搜索信息// 1,result.数据totalPage = result.getNumPages();//获取总页数 每页显示10条数据 currentPage = result.getPageIndex();//获取当前页ArrayList<MKPoiInfo> allPoi = result.getAllPoi();// 10条// 2. overlayPoiOverlay overlay = new PoiOverlay(SearchNearByActivity.this, mapView);overlay.setData(allPoi);// 3. mapview add overlaymapView.getOverlays().add(overlay);// 4. flashmapView.refresh();} else {}}});//发起搜索mksearch.poiSearchNearBy("电影院", centerszhm, 20000);}int  totalPage;int currentPage;//分页的操作@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubswitch (keyCode) {case KeyEvent.KEYCODE_5: // 下一页currentPage = ++currentPage % totalPage;//跳转到下一页mksearch.goToPoiPage(currentPage);break;case KeyEvent.KEYCODE_6: // 上一页currentPage -- ;if (currentPage < 0){currentPage = 0;Toast.makeText(getApplicationContext(), "已经第一页", 0).show();}mksearch.goToPoiPage(currentPage);break;default:break;}return super.onKeyDown(keyCode, event);}</span>


公交线路查询

 <span style="font-size:14px;">private void transitsearch() {mksearch.init(mapManager, new MKSearchAdapter() {@Overridepublic void onGetTransitRouteResult(MKTransitRouteResult result,int iError) {//获取数据 显示在mapview上if (iError == 0 && result != null) { //有搜索结果//1,数据MKTransitRoutePlan plan = result.getPlan(0);//2,overlayTransitOverlay overlay = new TransitOverlay(TransitSearchActivity.this ,mapView);overlay.setData(plan);//3 mapview add overlaymapView.getOverlays().add(overlay);//4 flushmapView.refresh();}}});/* * city - 城市名,用于在哪个城市内进行检索(必须填写) * start - 检索的起点,可通过关键字,坐标,两种方式指定 * end - 检索的终点,可通过关键字,坐标,两种方式指定  */MKPlanNode  start = new MKPlanNode();start.pt = centerszhm;MKPlanNode  end = new MKPlanNode();end.name = "世界之窗";mksearch.transitSearch("深圳", start, end);}</span>

驾车路线检索

<span style="font-size:14px;">private void driver() {mksearch.init(mapManager, new MKSearchAdapter() {@Overridepublic void onGetDrivingRouteResult(MKDrivingRouteResult result,int iError) {if (iError == 0 && result != null){//1 获取数据MKRoutePlan plan = result.getPlan(0);//2 创建overlay  设置数据RouteOverlay overlay = new RouteOverlay(DrivingSearchActivity.this,mapView);overlay.setData(plan.getRoute(0));//3 mapview add overlaymapView.getOverlays().add(overlay);//4 flushmapView.refresh();}}});/** * startCity - 起点所在城市,起点为坐标时可不填 * start - 搜索的起点,可以为坐标,名称任一种 * endCity - 终点所在城市,终点为坐标时可不填 * end - 搜索的终点,可以为坐标,名称任一种  */MKPlanNode start = new MKPlanNode();start.pt = centerszhm;MKPlanNode end = new MKPlanNode();end.name = "罗湖火车站";mksearch.drivingSearch("深圳", start, "深圳", end) ;}</span>

步行路线检索

<span style="font-size:14px;"> //  步行检索private void walk() {// TODO Auto-generated method stubmksearch.init(mapManager, new MKSearchAdapter() {@Overridepublic void onGetWalkingRouteResult(MKWalkingRouteResult result,int iError) {//1 数据MKRoutePlan plan = result.getPlan(0);//2 创建overlay  设置数据RouteOverlay overlay = new RouteOverlay(WalkingSearch.this,mapView);overlay.setData(plan.getRoute(0));//3 mapview add overlaymapView.getOverlays().add(overlay);//4 flushmapView.refresh();}});/*startCity - 起点所在城市,起点为坐标时可不填 * start - 搜索的起点,可以为坐标,名称任一种 * endCity - 终点所在城市,终点为坐标时可不填 * end - 搜索的终点,可以为坐标,名称任一种*/ MKPlanNode start = new MKPlanNode();start.pt = centerszhm;MKPlanNode end = new MKPlanNode();end.name = "世界之窗";mksearch.walkingSearch("深圳", start, "深圳", end);}</span>



0 0
原创粉丝点击