Android explore the development of Baidu map

@

Foreword

Before being involved in doing a small project to some content Baidu map, it was not the development process of Baidu map is not very understanding, so he looked up at the official document, and then read someone else's blog to learn, but found there was not I want something, official documents also did not speak in detail, ambiguous. So a bit behind their own way, will also share income out of their own learning.

Introduction map layers

Then directly get to the bar, on Baidu Map SDK integration is not here to explain, we first introduce the layers on the map.
First, we need to understand that the map is composed of several layers consisting of a map can contain one or more layers.
Each layer in each level is composed of a plurality of tile sheets, which cover the entire surface of the earth.
For example, you can see the streets of content including maps, points of interest, schools, parks and other show is a layer, additional traffic is to show through the layers to achieve.
So Baidu map provides us with a series of layers:
first you have to add MapView control in the layout, all subsequent operations are based on the control.

<com.baidu.mapapi.map.MapView
     android:id="@+id/mapView"
     android:layout_width="match_parent"
     android:layout_height="match_parent" />
//获得地图控制器
baiduMap = mapView.getMap();
baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);//卫星图层

By getMap () method to obtain the map controller, you can set the layer type, such as satellite layer, set to run after watching the effect:
Here Insert Picture Description
either layer:

//获得地图控制器
baiduMap = mapView.getMap();
baiduMap.setTrafficEnabled(true);//交通图层

Run to see the effect:
Here Insert Picture Description
related layers there are many, not repeat it describes the setting method is the same as above.

Maps covering introduction

Marker map covering that point, point A map marker is used to mark a location point information elements, each comprising:

  1. Marker point mark
  2. Flexible point mark ElasticMarker
  3. CircleMarker round mark
  4. Text Markup

The most common is a map point mark Marker, then how to achieve point markers on the map it?

//获得地图控制器
baiduMap = mapView.getMap();
LatLng point = new LatLng(39.963175, 116.400244);
BitmapDescriptor mit = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
OverlayOptions options = new MarkerOptions().position(point).icon(mit);
baiduMap.addOverlay(options);

First, get a map or controller, and then create a LatLng object is used to mark the location of a given point, the argument is the latitude and longitude, then generates a BitmapDescriptor by BitmapDescriptorFactory plant, which is a point mark icon is a picture, and then create objects MarkerOptions location and icons passed, and finally the options added to the map. Run to see the effect:
Here Insert Picture Description
This is a Marker mark.
Marker marks, as well as polygon mark except:

LatLng pt1 = new LatLng(39.93923, 116.357428);
LatLng pt2 = new LatLng(39.91923, 116.327428);
LatLng pt3 = new LatLng(39.89923, 116.347428);
List<LatLng> pts = new ArrayList<>();
pts.add(pt1);
pts.add(pt2);
pts.add(pt3);
OverlayOptions polygonOption = new PolygonOptions().points(pts).stroke(new Stroke(3, 0xAA00FF00)).fillColor(0xAAFFFF00);
baiduMap.addOverlay(polygonOption);

The principle is similar, there are defined three coordinate points, which is surrounded by a graphic triangle, quadrilateral if they want, you need to define the position of four points. Run to see the effect:
Here Insert Picture Description
Finally, what's the use of text markup:

LatLng llText = new LatLng(39.86923, 116.397428);
OverlayOptions textOption = new TextOptions()
        .bgColor(0xAAFFFF00)
        .fontSize(40)
        .fontColor(0xFFFF00FF)
        .text("百度地图SDK")
        .rotate(-30)
        .position(llText);
baiduMap.addOverlay(textOption);

Is quite simple, nothing more than some of the setting properties, see the direct effect of that operation:
Here Insert Picture Description
the same number of other marks usage, do not do too much introduction.

Map Events

So here is what you map the event, used Baidu map students know, certainly can click on the map, and you point at which position, the position of the corresponding point mark will appear, then this function is how to achieve it? In fact, very simple, with the event to complete the map, we work together to achieve this:

baiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
        BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
        OverlayOptions options = new MarkerOptions().position(latLng).icon(descriptor);
        baiduMap.addOverlay(options);
    }

    @Override
    public boolean onMapPoiClick(MapPoi mapPoi) {
        return false;
    }
});

Call setOnMapClickListener () method to register the listener, listening method, create a Marker mark and added to the map, this time, just click anywhere on the map, it will trigger a click event, and draw Marker mark at that position.
So there is a marked Marker click events, you can get some information about that location by clicking on the Marker mark:

baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {
        Toast.makeText(getApplicationContext(), marker.getPosition().toString(), Toast.LENGTH_LONG).show();
        return false;
    }
});

Here we look at the output of the latitude and longitude Maker marker position, now run to see results:
Here Insert Picture Description

POI search

POI is an abbreviation for "Point of Interest", the Chinese can be translated as "points of interest." In GIS, a POI can be a house, a shop, a mailbox, a bus station. Then we can query some of the points of interest on the map and feedback to the user to retrieve information via POI.
So here I introduce two search, retrieval and surrounding cities retrieval, similar to other search functions.
First, implement a menu, the menu bar has surrounding urban search and retrieval are two options, the code is relatively simple, do not stick out, our focus is to retrieve.

PoiSearch mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
    @Override
    public void onGetPoiResult(PoiResult poiResult) {
        List<PoiInfo> allPoi = poiResult.getAllPoi();
        for (PoiInfo poiInfo : allPoi) {
            Log.d("MainActivityClass", poiInfo.name.toString());
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) { 
    }
    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }
});

To create PoiSearch first instance to retrieve, and then register a listener, when initiating retrieve and return data will call onGetPoiResult () method, which returns a PoiResult object that calls getAllPoi () method to get all the information retrieval success here we look at the output retrieve the object's name.
After registering event listeners, we have to realize retrieval:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.city_search:
            mPoiSearch.searchInCity(new PoiCitySearchOption()
                    .city("北京") //城市名
                    .keyword("餐厅")  //检索关键字
                    .pageNum(1)     //结果页
                    .pageCapacity(10)); //结果数
            break;
    }
    return true;
}

Or call searchInCity method to achieve the target by PoiSearch city search, now run to see results:
Here Insert Picture Description
log information is as follows:

08-01 15:32:05.882 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 利群烤鸭店(前门店)
08-01 15:32:05.882 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 金鼎轩(团结湖店)
08-01 15:32:05.882 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 金鼎轩(方庄店)
08-01 15:32:05.882 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 眉州东坡酒楼(国奥村店)
08-01 15:32:05.883 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 便宜坊(鲜鱼口店)
08-01 15:32:05.883 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 眉州东坡酒楼(亚运村店)
08-01 15:32:05.883 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 海底捞火锅(红庙店)
08-01 15:32:05.883 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 铃木食堂(杨梅竹店)
08-01 15:32:05.883 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 玫瑰花园(远大路店)
08-01 15:32:05.883 25962-25962/com.itcast.optimizedemo D/MainActivityClass: 海底捞火锅(西直门店)

Successfully get to the restaurant information.
Then try vicinity search:

@Override
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) {
        case R.id.city_search:
            LatLng pt = new LatLng(39.93923, 116.357428);
            mPoiSearch.searchNearby(new PoiNearbySearchOption()
                    .radius(10000)  //检索半径
                    .pageCapacity(10)   //结果数
                    .pageNum(1)         //结果页
                    .location(pt)       //检索点
                    .keyword("ATM"));   //检索关键字
            break;
    }
    return true;
}

Listening without modifying the code, by simply retrieving peripheral searchNearby () method may be, after the operation, the log information is as follows:

08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 兴业银行24小时自助银行(北京月坛支行)
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 北京银行24小时自助银行(车公庄大街社区支行)
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 平安银行(北京官园自助银行)
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 浦发银行24小时自助银行
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 中国邮政储蓄银行24小时自助银行服务
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 中国邮政储蓄银行24小时自助银行(新华里支行)
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 招商银行ATM(人民医院西直门ATM)
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 北京银行ATM
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 中国工商银行ATM(北京大学人民医院科研教学楼东南)
08-01 15:36:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 远通维景国际大酒店-自动取款机

Successfully acquired cash machine information.
So by POI search and Marker tag, we can further our design applications, such as ATM location will be marked on the map after searching the ATM machine to alert the user. Then we look to achieve this functionality:

BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka);
OverlayOptions options = new MarkerOptions().position(poiInfo.location).icon(descriptor);
baiduMap.addOverlay(options);

The code is very simple, the rest of the code without modification, just add the code above method in the listener listens search, the search target contains a lot of information, including location, then we can set the mark by location. Run to see the effect:
Here Insert Picture Description
When we click on the periphery retrieve and display the location of the ATM machine on the map.
So after learning these above knowledge, we can achieve these locations to display information on a map based on keywords users search, for example, a user searches for restaurants, the map on the line marked out all the restaurants, and when the user clicks on a Marker after labeling, display some of the information specified restaurant by listening event, such as a restaurant name, address, and so the average consumer, thus greatly enriched our application.
So for this feature, Baidu Maps also provides related API to help us simplify programming, we take a look (here for example to food details):
First, define a class that inherits from PoiOverlay:

    class MyOverlay extends PoiOverlay {

        /**
         * 构造函数
         *
         * @param baiduMap 该 PoiOverlay 引用的 BaiduMap 对象
         */
        public MyOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onPoiClick(int i) {
            List<PoiInfo> allPoi = getPoiResult().getAllPoi();
            PoiInfo info = allPoi.get(i);
            if (info.hasCaterDetails) {//判断是否有详情
                mPoiSearch.searchPoiDetail(new PoiDetailSearchOption().poiUid(info.getUid()));
            }
            return super.onPoiClick(i);
        }
    }

Here used to retrieve restaurant details after successfully retrieving calls onGetPoiDetailResult () method, which returns a PoiDetailResult object in it encapsulates some of the details of the restaurant:

@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
Log.d("MainActivityClass", poiDetailResult.getPrice() + "");
Log.d("MainActivityClass", poiDetailResult.getTelephone());   
}

Here the output of the phone and the price of the restaurant, after running log information is as follows:

08-01 16:10:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: 102.0
08-01 16:10:01.921 27315-27315/com.itcast.optimizedemo D/MainActivityClass: (010)51816880 (010)51816876

There are more details here do not do the presentation.
Some students might proceeding to this step, it was found PoiOverlay class can not be found, because starting from the 3.6.0 version, PoiOverlay classes are no longer integrated into the jar package, so we need to download the official Demo instance, then the instance Code overlayutil folder will copy and paste into your project can be.

Bus lines inquiry

Bus and our lives, we always need to know some cases bus lines, how to get information on bus routes do?

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.city_search:
                mPoiSearch.searchInCity(new PoiCitySearchOption()
                        .keyword("201")   //公交路线
                        .city("南昌")
                        .scope(2));
                break;

Must first retrieve the bus lines, for example, I searched here Nanchang 201 bus, scope here is to add an attribute necessary to start from the 5.1.0 version of the SDK, if your version is that the retrieval before 5.1.0 way may not be the same, please Tell me what network document describes specific.
So after a search, it will call onGetPoiResult () method, as in this and previous:

@Override
public void onGetPoiResult(PoiResult poiResult) {
      if (poiResult == null || poiResult.error != SearchResult.ERRORNO.NO_ERROR) {
            return;
      }
      List<PoiInfo> poiInfos = poiResult.getAllPoi();
      mBusLineSearch.searchBusLine(new BusLineSearchOption()
                .city("南昌")
                .uid(poiInfos.get(0).getUid()));

Subsequently obtained by getAllPoi () to retrieve information, and then create BusLineSearch object () obtained by the method newInstance. And then to line inquiries by calling searchBusLine () method. Finally, we need to register event listeners BusLineSearch:

mBusLineSearch.setOnGetBusLineSearchResultListener(new OnGetBusLineSearchResultListener() {
            @Override
            public void onGetBusLineResult(BusLineResult busLineResult) {
                if (busLineResult == null || busLineResult.error != SearchResult.ERRORNO.NO_ERROR) {
                    return;
                }
                List<BusLineResult.BusStation> stations = busLineResult.getStations();
                for (BusLineResult.BusStation station : stations) {
                    Log.e("MainActivityClass", station.getTitle());
                }
            }
        });

Here the amount of output bus lines bus station name, we look at the log information to run the program:

08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 老福山花园东
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 自来水公司
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 坛子口立交南
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 徐坊客运站
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 新溪桥
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 和坊西路
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 三店西路口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 省皮肤病医院(机电职院青云谱校区)
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 江铃西二路(东)口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 梨园小区②
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 武警医院
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 迎宾·京山南路口(原昌南大道口站)
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 陈云故居小区(招呼站)
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 迎宾·定山路口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 印钞厂
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 迎宾·庄园路口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 下邓新村
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 江西信息学院
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 南昌邮区中心局
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 昌南公交枢纽
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 豪泰小兰工业园
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 小蓝一路东口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 澄湖北大道站
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 迎宾·小蓝二路口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 省送变电公司
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 莲西
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 莲西·向阳路口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 斗门
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 澄碧湖公园南门
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 南昌县人民医院
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 府前东路东口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 玺园
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 八一乡桥头
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 省农科院
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 江西生物学院
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 莲塘北大道(江西技师学院)
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 莲塘·小蓝北路口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 墨山立交南
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 墨山立交北
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 朱桥·城南大道口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 熊坊
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 南莲社区北
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 包家花园
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 何家坊
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 三店西路东口
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 新溪桥
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 徐坊客运站
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 坛子口立交南
08-01 16:42:49.257 10339-10339/com.itcast.optimizedemo E/MainActivityClass: 老福山花园

Successful sites along the way, you can get more information about the bus stop, do not make the presentation.

Route planning

There we will find when using Baidu map, when you enter your departure point and destination of the time, the map will draw routes that can be divided into:

  1. bus routes
  2. Driving routes
  3. Walking the line

So how are we going to achieve route planning?
Route planning and in front of POI search is very similar:
Here Insert Picture Description
This is the menu are three kinds of tests to route planning.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //起点
        PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", "西二旗地铁站");
        //终点
        PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", "百度科技园");
        switch (item.getItemId()) {
            case R.id.bus:
                mSearch.transitSearch(new TransitRoutePlanOption()
                        .city("北京")
                        .from(stNode)
                        .to(enNode));
                break;
            case R.id.walking:
                mSearch.drivingSearch(new DrivingRoutePlanOption()
                        .from(stNode)
                        .to(enNode));
                break;
            case R.id.driver:
                mSearch.walkingSearch(new WalkingRoutePlanOption()
                        .from(stNode)
                        .to(enNode));
                break;
        }
        return true;
    }

First create RoutePlanSearch object.
Since it is a route planning, then there is the start position and end position, here to set the start and end points by PlanNode the object, and then retrieve three kinds of planning the way, is the need to pass a city bus planning parameters, the other two is basically the same. Of course, route planning way far more than three kinds of riding as well as planning, etc., can be divided into general planning riding and riding electric Juji line, in short, very rich function, you can look at specific official documents. So after retrieval is complete, we need to implement RoutePlanSearch monitor events:

mSearch = RoutePlanSearch.newInstance();
        mSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
                if (walkingRouteResult.error == SearchResult.ERRORNO.NO_ERROR) {
                    baiduMap.clear();   //清空地图
                    WalkingRouteOverlay overlay = new MyWalkingRouteOverlay(baiduMap);  
                    baiduMap.setOnMarkerClickListener(overlay);
                    //获取路径规划数据,(以返回的第一条路线为例)
                    overlay.setData(walkingRouteResult.getRouteLines().get(0));
                    //在地图上绘制BikingRouteOverlay
                    overlay.addToMap();
                    overlay.zoomToSpan();
                }
            }

            @Override
            public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
                if (transitRouteResult.error == SearchResult.ERRORNO.NO_ERROR) {
                    baiduMap.clear();
                    TransitRouteOverlay overlay = new MyTransitRouteOverlay(baiduMap);
                    baiduMap.setOnMarkerClickListener(overlay);
                    overlay.setData(transitRouteResult.getRouteLines().get(0));
                    overlay.addToMap();
                    overlay.zoomToSpan();
                }
            }

            @Override
            public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

            }

            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
                if (drivingRouteResult.error == SearchResult.ERRORNO.NO_ERROR) {
                    baiduMap.clear();
                    DrivingRouteOverlay overlay = new MyDrivingRouteOverlay(baiduMap);
                    baiduMap.setOnMarkerClickListener(overlay);
                    overlay.setData(drivingRouteResult.getRouteLines().get(0));
                    overlay.addToMap();
                    overlay.zoomToSpan();
                }
            }

            @Override
            public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

            }

            @Override
            public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

            }
        });

Achieve each mode are similar, not only is the use of a different class, then each way corresponds implement a custom class, also posted here:

    class MyWalkingRouteOverlay extends WalkingRouteOverlay {

        public MyWalkingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onRouteNodeClick(int i) {
            return super.onRouteNodeClick(i);
        }
    }

    class MyTransitRouteOverlay extends TransitRouteOverlay{

        public MyTransitRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onRouteNodeClick(int i) {
            return super.onRouteNodeClick(i);
        }
    }

    class MyDrivingRouteOverlay extends DrivingRouteOverlay{

        public MyDrivingRouteOverlay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onRouteNodeClick(int i) {
            return super.onRouteNodeClick(i);
        }
    }

Here I do not have to implement specific functions of these methods, interested can go try to write about.
Well, now run to see results;
Here Insert Picture Description
on the map will be drawn Directions beginning to the end, not the same route each way, you can try it for yourself.

Geocoding

Sometimes developers will take into account the need for a corresponding conversion between the latitude and longitude and address, Baidu Map SDK also provides us with the appropriate API, geocoding and reverse geocoding:
Here I also added two sub-menu bar menu for testing.
GeoCoder first create an object:

GenCoder mGeoCoder = GeoCoder.newInstance();

Then initiates retrieval:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.geo:
                mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption()
                        .location(new LatLng(39.946758, 116.423134)));
                break;
            case R.id.reverse:
                mGeoCoder.geocode(new GeoCodeOption()
                        .address("百度园").city("北京"));
                break;
        }
        return true;
    }

After retrieving successful, we need to listen to GenCoder:

        mGeoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                Log.d("MainActivityClass", geoCodeResult.getLocation().toString());
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                Log.d("MainActivityClass", reverseGeoCodeResult.getAddress());
            }
        });

Here it is a bit simple output information after the conversion, run the program, when we click on geocoding, log information is as follows:

08-01 17:37:18.249 23338-23338/com.itcast.optimizedemo D/MainActivityClass: 北京市东城区交道口东大街4-8

When we click reverse geocoding, log information is as follows:

08-01 17:40:18.249 23338-23338/com.itcast.optimizedemo D/MainActivityClass: latitude: 39.946758 longitude: 116.423134

Here, on the development of Baidu map will introduce over, we should have put some basic map features are introduced to the open, if there are questions, please correct me.
Test code has been uploaded, need can be downloaded.
GitHub Download

Guess you like

Origin www.cnblogs.com/blizzawang/p/11411626.html
Recommended