美文网首页
百度地图学习笔记

百度地图学习笔记

作者: Owen270 | 来源:发表于2017-11-05 15:35 被阅读25次

参考链接1:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/coorinfo

参考链接2:http://lbsyun.baidu.com/index.php?title=uri/api/android

1.百度地图之定位,反地址搜索,poi地址搜索(1.从近到远 2.城市内部关键词搜索设置),行政区域搜索(按城市名称)

1.1.poi从近到远地址搜索效果图

图片.png

1.2.poi城市内部关键词搜索设置

图片.png

1.3.行政区域搜索(按城市名称)

图片.png
具体代码如下:

public class MySelectAddress extends Activity implements View.OnClickListener,MyIndicator.OnIndiacatorClickListener,OnGetPoiSearchResultListener,MyOrientationListener.OnOrientationListener,OnGetGeoCoderResultListener,OnGetDistricSearchResultListener{
    @BindView(R.id.mapview)
    MapView mMapView;
    @BindView(R.id.recyclerview_map)
    RecyclerView mRecyclerView;
    @BindView(R.id.indicator_map)
    MyIndicator myIndicator;
    @BindView(R.id.recyclerview_search)
    RecyclerView mRecycler_Search;
    @BindView(R.id.edit_city)
    EditText mEdit_City;
    @BindView(R.id.tv_city)
    TextView mTv_City;
    @BindView(R.id.tv_cancle)
    TextView mTv_cancle;
    @BindView(R.id.image_ddz)
    ImageView mImage_Center;
    @BindView(R.id.view_bg)
    LinearLayout view_bg;
    @BindView(R.id.image_clean)
    ImageView mClean;
    @BindView(R.id.linear_search_city)
    LinearLayout mLinear_Search;
    @BindView(R.id.image_location)
    ImageView image_location;

    /**
     * 城市
     */
    private LinearLayoutManager mLinearLayoutManager;
    @BindView(R.id.city_layout)
    RelativeLayout city_layout;
    @BindView(R.id.recycler_city)
    RecyclerView recycler_city;
    private String[] mLinearList = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    @BindView(R.id.linear_check)
    LinearLayout mLinear_Check;
    @BindView(R.id.tv_zhishiqi)
    TextView mTextView;
    @BindView(R.id.tv_address_city_cont)
    TextView tv_address_city;
    @BindView(R.id.image_city)
    ImageView image_city;
    @BindView(R.id.linear_location_city)
    LinearLayout linear_location_city;
    @BindView(image_refresh_city)
    ImageView mImage_Refresh_city;
    @BindView(R.id.image_sy_city)
    ImageView mImage_Sy_city;
    @BindView(R.id.linear_cache)
    LinearLayout linear_cache;
    @BindView(R.id.linear_city)
    LinearLayout linear_city;
    private CityAdapter mCityAdapter;
    private boolean mCheck=true;
    private SearchAddressAdapter mSearchAddressAdapter;
    /***
     * 反地址搜索
     */
    private GeoCoder mGeoSearch = GeoCoder.newInstance();
    /**
     * 附近搜索type
     */
    private PoiSearch mPoiSearch = PoiSearch.newInstance();
    /**
     * 城市内部关键词搜索输入内容
     */
    private PoiSearch mPoiSearchEdit = PoiSearch.newInstance();
    /***
     * 行政区域搜索
     */
    private DistrictSearch mPoiSearchCity = DistrictSearch.newInstance();
    /**
     * 搜索类型---写字楼,学校,小区,市区内部按照关键词搜索,行政区域搜索
     */
    private ReverseGeoCodeOption  mPoiRevers;
    private PoiNearbySearchOption mPoiPlot;
    private PoiNearbySearchOption mPoiOffice;
    private PoiNearbySearchOption mPoiSchool;
    private PoiCitySearchOption   mPoiInCity;
    private DistrictSearchOption mDistrictOption;
    private int mRange=2000;//搜索范围
    /****
     * 定位
     */
    private LocationClient mLocationClient;
    /**
     * 定位监听
     */
    public BDLocationListener myListener = new MyLocationListener();
    private   BaiduMap mBaiduMap=null;

    private boolean isFistLocation=true;
    /***
     * 陀螺仪最后的x
     */
    private float mLastX=0;
    /**
     * 当前位置金纬度
     */
    private LatLng mLocationLat=null;
    /**
     * 陀螺仪
     */
    private MyOrientationListener mMyOrientationListener;

    /**
     * 最后一次移动位置金纬度
     */
    private LatLng mLastLat;

    /**
     * 每页多少个
     */
    private int mPageSize = 20;
    /**
     * 当前搜索0全部 1写字楼 2小区 3学校
     */
    private int mType = 0;

    private MapAdapter mMapAdapter;

    /**
     * 是否开启城市修改
     */
    private boolean isChooseCity = false;
    private String mCheckCity="";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_select_address);
        ButterKnife.bind(this);
        initRecycler();
        initMap();
        initMyLoc();
        initSearchOption();
        initCity();
        initSearchEdit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.image_location:
                if(mLocationLat!=null){
                    showMap(mLocationLat.latitude,mLocationLat.longitude);
                }
                break;
            case R.id.linear_city:
                mCheck=!mCheck;
                Utils.rotation(image_city,mCheck);
                mEdit_City.setText("");
                mEdit_City.setFocusable(false);//失去焦点
                //恢复mEdit_City可以点击,不会出发onFouchChangeListener
                mEdit_City.setFocusable(true);
                mEdit_City.setFocusableInTouchMode(true);
                KeyboardUtils.hideSoftInput(this);
                if(mCheck){
                    city_layout.setVisibility(View.GONE);
                }else{
                    city_layout.setVisibility(View.VISIBLE);
                }

                break;
            case R.id.linear_location_city:
                mImage_Refresh_city.setVisibility(View.VISIBLE);
                mImage_Sy_city.setVisibility(View.GONE);
                isChooseCity=true;
                break;
            case R.id.tv_address_city_cont:
                mCheck = !mCheck;
                Utils.rotation(image_city, mCheck);
                if (mCheck) {
                    city_layout.setVisibility(View.GONE);
                } else {
                    city_layout.setVisibility(View.VISIBLE);
                }
                mTv_City.setText(tv_address_city.getText().toString());
                mPoiSearchCity.searchDistrict(mDistrictOption
                        .cityName(tv_address_city.getText().toString()));

                break;
            case R.id.tv_cancle:
                mEdit_City.setText("");
                mEdit_City.setFocusable(false);//失去焦点
                //恢复mEdit_City可以点击
                mEdit_City.setFocusable(true);
                mEdit_City.setFocusableInTouchMode(true);
                KeyboardUtils.hideSoftInput(this);
                break;
        }
    }
    /**
     * 输入搜索初始化
     */
    private void initSearchEdit() {
        mEdit_City.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    Log.i("TAG","hasFocus:"+hasFocus);
                    if(!mCheck){//
                        mCheck=true;
                        Utils.rotation(image_city,mCheck);
                    }
                    mTv_cancle.setVisibility(View.VISIBLE);
                    city_layout.setVisibility(View.GONE);
                    view_bg.setVisibility(View.VISIBLE);
                    mRecycler_Search.setVisibility(View.VISIBLE);
                }else{
                    Log.i("TAG","hasFocuss:"+hasFocus);
                    mTv_cancle.setVisibility(View.GONE);
                    city_layout.setVisibility(View.GONE);
                    view_bg.setVisibility(View.GONE);
                    mRecycler_Search.setVisibility(View.GONE);
                }
            }
        });

        /**
         * 输入搜索
         */
        mPoiSearchEdit.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                if (poiResult.getAllPoi() != null) {
                    view_bg.setVisibility(View.GONE);
                    if (poiResult.getAllPoi().size() > 0) {
                        mRecycler_Search.setVisibility(View.VISIBLE);
                        mSearchAddressAdapter.setData(poiResult.getAllPoi());
                        mSearchAddressAdapter.notifyDataSetChanged();
                        view_bg.setVisibility(View.GONE);
                        linear_cache.setVisibility(View.GONE);
                    } else {
                        view_bg.setVisibility(View.VISIBLE);
                        mRecycler_Search.setVisibility(View.GONE);
                    }
                } else {
                    //view_bg.setVisibility(View.VISIBLE);
                    mSearchAddressAdapter.setData(null);
//                    if (isStartSearch)
//                        view_bg.setVisibility(View.VISIBLE);
//                    mSearchAddressAdapter.setData(new ArrayList<>());
                }
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        });
        mEdit_City.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPoiInCity.city(mTv_City.getText().toString().trim());
                mPoiInCity.keyword(mEdit_City.getText().toString().trim());
                mPoiSearchEdit.searchInCity(mPoiInCity);
                if (s.length() != 0) {
                    mClean.setVisibility(View.VISIBLE);
                } else {
                    mClean.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }
    private void initCity() {
        //添加textview
        for (int i = 0; i < mLinearList.length; i++) {
            TextView mTextView = new TextView(MySelectAddress.this);
            mTextView.setTextColor(getResources().getColor(R.color.tab_press_text));
            mTextView.setTextSize(11);
            mTextView.setText(mLinearList[i]);
            mTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            mLinear_Check.addView(mTextView);
        }
        mLinear_Check.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int i = ((int) (event.getY() / (mLinear_Check.getMeasuredHeight() / 26))) - 1;
                if (i < mLinearList.length) {
                    if (i > -1) {
                        mTextView.setVisibility(View.VISIBLE);
                        mTextView.setText(mLinearList[i]);
                        for (int j = 0; j < DemoApplication.mCity.size(); j++) {
                            if (DemoApplication.mCity.get(j).getCity().equals(mLinearList[i])) {
                                moveToPosition(j);
                                break;
                            }
                        }
                    }
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    mTextView.setVisibility(View.GONE);
                }
                return true;
            }
        });
    }

    /**
     * 移动
     *
     * @param n
     */
    private void moveToPosition(int n) {
        int firstItem = mLinearLayoutManager.findFirstVisibleItemPosition();
        int lastItem = mLinearLayoutManager.findLastVisibleItemPosition();
        Log.i("TAG","firstItem:"+firstItem);
        Log.i("TAG","lastItem:"+lastItem);
        if (n <= firstItem) {
            recycler_city.scrollToPosition(n);
        } else if (n <= lastItem) {
            int top = recycler_city.getChildAt(n - firstItem).getTop();
            recycler_city.scrollBy(0, top);
        } else {
            recycler_city.scrollToPosition(n);
        }
    }


    /***
     * tab监听
     * @param i
     * @param view
     */
    @Override
    public void onClick(int i, View view) {
        Log.i("TAG","A"+i);
        mType=i;
        searchNeayBy(mLastLat);
    }

    /**
     * 初始化地图
     */
    private void initMap() {
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        //交通
        mBaiduMap.setTrafficEnabled(true);
        mBaiduMap.setMyLocationEnabled(true);
        mLocationClient = new LocationClient(getApplicationContext());
        mLocationClient.registerLocationListener(myListener);

        /**
         * 隐藏图标
         */
        View child = mMapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            // child.setVisibility(View.INVISIBLE);
        }
        /**
         * 隐藏比例尺
         */
        //mMapView.showScaleControl(false);
        //每隔1000ms定位一次
        MapUtils.initMap(mLocationClient, 1000);
        //initListener();
    }

    /**
     * 初始化recyclerview
     */
    private void initRecycler() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mMapAdapter = new MapAdapter());
        recycler_city.setLayoutManager(mLinearLayoutManager=new LinearLayoutManager(this));
        recycler_city.setAdapter(mCityAdapter=new CityAdapter(DemoApplication.mCity));
        mCityAdapter.notifyDataSetChanged();
        mRecycler_Search.setLayoutManager(new LinearLayoutManager(this));
        mRecycler_Search.setAdapter(mSearchAddressAdapter=new SearchAddressAdapter());
    }

    @Override
    public void onGetPoiResult(PoiResult poiResult) {
        Log.i("TAG","C1");
        if(poiResult.getAllPoi()!=null){
            mMapAdapter.setData(poiResult.getAllPoi());
            mMapAdapter.notifyDataSetChanged();
        }else{
            mMapAdapter.setData(null);
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }

    @Override
    public void onOrientationChanged(float x) {
        this.mLastX=x;
    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
        if(reverseGeoCodeResult.getPoiList()!=null){
            mMapAdapter.setData(reverseGeoCodeResult.getPoiList());
            mMapAdapter.notifyDataSetChanged();
        }else{
            mMapAdapter.setData(null);
        }
    }

    /***
     * 区域搜索
     * @param districtResult
     */
    @Override
    public void onGetDistrictResult(DistrictResult districtResult) {
        if(districtResult!=null){
            showMap(districtResult.centerPt.latitude,districtResult.centerPt.longitude);
        }

    }

    /**
     * 定位监听
     */
    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(final BDLocation location) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (location.getLocType() == BDLocation.TypeServerError) {
                        Toast.makeText(getApplicationContext(), "请您检查是否禁用获取位置信息权限,尝试重新请求定位~", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    /**
                     * 如果第一次定位添加marker
                     */
                    if (isFistLocation) {
                        showMap(location.getLatitude(), location.getLongitude());
                        isFistLocation = false;
                    }
                    mLocationLat = new LatLng(location.getLatitude(), location.getLongitude());
                    MyLocationData locData = new MyLocationData.Builder()
                            .accuracy(location.getRadius())
                            .direction(mLastX)
                            .latitude(location.getLatitude())
                            .longitude(location.getLongitude())
                            .build();
                    mBaiduMap.setMyLocationData(locData);
                    //使用默认图标
                    MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true,null);
                    mBaiduMap.setMyLocationConfiguration(config);
                    tv_address_city.setText(location.getCity());
                    if(isChooseCity){
                        mImage_Refresh_city.setVisibility(View.GONE);
                        mImage_Sy_city.setVisibility(View.VISIBLE);
                        isChooseCity=false;
                    }
                    if(TextUtils.isEmpty(mCheckCity)){
                        mTv_City.setText(location.getCity());
                        mCheckCity=location.getCity();
                    }else{
                        mTv_City.setText(mCheckCity);
                    }
                }
            });
        }
    }



    /**
     * 移动地图
     */
    private void showMap(double latitude, double longtitude) {
        LatLng mLatLng = new LatLng(latitude, longtitude);
        MapStatus mMapStatus = new MapStatus.Builder()
                .target(mLatLng)
                .zoom(16)
                .build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        mBaiduMap.animateMapStatus(mMapStatusUpdate);
        searchNeayBy(mLatLng);
    }
    /**
     * 搜索周边地理位置
     */
    private void searchNeayBy(LatLng point) {
        if (point == null)
            return;
        mLastLat = point;
        switch (mType) {
            case 0://全部地址,根据经纬度反地址搜索
                mGeoSearch.reverseGeoCode(mPoiRevers
                        .location(point));
                Log.i("TAG","B1");
                break;
            case 1://写字楼
                Log.i("TAG","B2");
                mPoiOffice.location(point);
                mPoiSearch.searchNearby(mPoiOffice);
                break;
            case 2://小区
                Log.i("TAG","B3");
                mPoiPlot.location(point);
                mPoiSearch.searchNearby(mPoiPlot);
                break;
            case 3://学校
                Log.i("TAG","B4");
                mPoiSchool.location(point);
                mPoiSearch.searchNearby(mPoiSchool);
                break;

        }

    }


    /**
     * 初始化搜索类型s
     */
    private void initSearchOption() {

//        mPoiAll = new PoiNearbySearchOption();
//        mPoiAll.sortType(PoiSortType.distance_from_near_to_far);
//        mPoiAll.radius(mRange);
//        mPoiAll.pageCapacity(mPageSize);
//        mPoiAll.pageNum(0);
//        mPoiAll.keyword("房子");
        mPoiRevers=new ReverseGeoCodeOption();
        mPoiRevers.location(null);


        mPoiOffice = new PoiNearbySearchOption();
        mPoiOffice.keyword("写字楼");
        mPoiOffice.sortType(PoiSortType.distance_from_near_to_far);
        mPoiOffice.radius(mRange);
        mPoiOffice.pageCapacity(mPageSize);
        mPoiOffice.pageNum(0);
        mPoiOffice.location(null);


        mPoiPlot = new PoiNearbySearchOption();
        mPoiPlot.sortType(PoiSortType.distance_from_near_to_far);
        mPoiPlot.radius(mRange);
        mPoiPlot.pageCapacity(mPageSize);
        mPoiPlot.pageNum(0);
        mPoiPlot.keyword("小区");
        mPoiPlot.location(null);


        mPoiSchool = new PoiNearbySearchOption();
        mPoiSchool.sortType(PoiSortType.distance_from_near_to_far);
        mPoiSchool.radius(mRange);
        mPoiSchool.pageCapacity(mPageSize);
        mPoiSchool.pageNum(0);
        mPoiSchool.keyword("学校");
        //城市内部关键词搜索设置
        mPoiInCity=new PoiCitySearchOption();
        mPoiInCity.city(null);//根据用户输入动态配置
        mPoiInCity.keyword(null);////根据用户输入动态配置
        mPoiInCity.pageCapacity(mPageSize);
        //行政区域搜索设置
        mDistrictOption=new DistrictSearchOption();
        mDistrictOption.cityName(null);//根据用户选择进行设置

        mGeoSearch.setOnGetGeoCodeResultListener(this);
        mPoiSearch.setOnGetPoiSearchResultListener(this);
        mPoiSearchCity.setOnDistrictSearchListener(this);
        image_location.setOnClickListener(this);
        linear_city.setOnClickListener(this);
        linear_location_city.setOnClickListener(this);
        tv_address_city.setOnClickListener(this);
        myIndicator.setIndiacatorListener(this);
        mTv_cancle.setOnClickListener(this);
        mCityAdapter.setmOnItemOnClickListener(new CityAdapter.OnItemOnClickListener() {
            @Override
            public void onClick(int position, View view) {
                mCheck = !mCheck;
                Utils.rotation(image_city, mCheck);
                if (mCheck) {
                    city_layout.setVisibility(View.GONE);
                } else {
                    city_layout.setVisibility(View.VISIBLE);
                }
                mCheckCity=DemoApplication.mCity.get(position).getCity();
                mTv_City.setText(mCheckCity);
                mPoiSearchCity.searchDistrict(mDistrictOption
                        .cityName(DemoApplication.mCity.get(position).getCity()));


            }
        });
        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            @Override
            public void onMapStatusChangeStart(MapStatus mapStatus) {

            }

            @Override
            public void onMapStatusChangeStart(MapStatus mapStatus, int i) {

            }

            @Override
            public void onMapStatusChange(MapStatus mapStatus) {

            }

            @Override
            public void onMapStatusChangeFinish(MapStatus mapStatus) {
                if(mapStatus!=null){
                    translate(mImage_Center);
                    searchNeayBy(mapStatus.target);
                }
            }
        });

    }

    /**
     * 图片动画
     *
     * @param view
     */
    private void translate(View view) {
        TranslateAnimation anim = new TranslateAnimation(0, 0, 20, 0);
        anim.setDuration(200);
        anim.setRepeatCount(2);
        anim.setRepeatMode(Animation.REVERSE);
        view.startAnimation(anim);
    }
    @Override
    protected void onStart() {
        super.onStart();
        mBaiduMap.setMyLocationEnabled(true);
        if(!mLocationClient.isStarted()){
               new RxPermissions(this).requestEach(Manifest.permission.ACCESS_COARSE_LOCATION).subscribe(new Subscriber<Permission>() {
                   @Override
                   public void onCompleted() {

                   }

                   @Override
                   public void onError(Throwable e) {

                   }

                   @Override
                   public void onNext(Permission permission) {
                       if(permission.granted){
                           mLocationClient.start();
                           mMyOrientationListener.start();
                       } else if (permission.shouldShowRequestPermissionRationale) {
                           Toast.makeText(getApplicationContext(),"请允许定位权限",Toast.LENGTH_LONG).show();
                       } else {
                           Toast.makeText(getApplicationContext(),"请允许定位权限,需要手动设置",Toast.LENGTH_LONG).show();
                           startActivity(Utils.getAppDetailSettingIntent());
                       }
                   }
               });
        }
    }

    /**
     * 初始化陀螺仪
     */
    private void initMyLoc() {
        mMyOrientationListener = new MyOrientationListener(this);
        mMyOrientationListener.setOnOrientationListener(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();

    }
    @Override
    protected void onStop() {
        super.onStop();
        mLocationClient.stop();
        mBaiduMap.setMyLocationEnabled(false);
        mMyOrientationListener.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        mGeoSearch.destroy();
        mPoiSearch.destroy();
        mPoiSearchCity.destroy();
        mPoiSearchEdit.destroy();
    }
}

2.百度地图只步行路径规划

2.1.效果图

图片.png
2.2具体代码如下:
public class MyRoutePlanActivity extends AppCompatActivity implements OnGetRoutePlanResultListener,View.OnClickListener{
     @BindView(R.id.mapview_shop_map)
     MapView mapView;
     BaiduMap baiduMap;
     @BindView(R.id.tv_daohang)
     LinearLayout tv_daohang;
    /**
     * 步行路径规划
     */
    RoutePlanSearch mRoutePlanSearch=RoutePlanSearch.newInstance();
    private double startLat=22.590164;
    private double startLon=113.977696;
    private double endLat=22.545682;
    private double endLon=113.923962;
    PlanNode stNode;
    PlanNode enNode;
    WalkingRoutePlanOption mPlanOption;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_plan);
        ButterKnife.bind(this);
        baiduMap=mapView.getMap();
        mapView.getChildAt(2).setPadding(0, 0, 30, 90);//这是控制缩放控件的位置
        /***
         * 步行路径规划配置
         */
        mPlanOption=new WalkingRoutePlanOption();
        stNode=PlanNode.withLocation(new LatLng(startLat,startLon));
        enNode=PlanNode.withLocation(new LatLng(endLat,endLon));
        mPlanOption.from(stNode).to(enNode);
        mRoutePlanSearch.setOnGetRoutePlanResultListener(this);
        mRoutePlanSearch.walkingSearch(mPlanOption);
        tv_daohang.setOnClickListener(this);
    }

    @Override
    public void onGetWalkingRouteResult(WalkingRouteResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(getApplicationContext(),"抱歉,未找到结果",Toast.LENGTH_LONG).show();
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            //起终点或途经点地址有岐义,通过以下接口获取建议查询信息
            result.getSuggestAddrInfo();
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            WalkingRouteOverlay overlay = new WalkingRouteOverlay(baiduMap);
            baiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(result.getRouteLines().get(0));
            overlay.addToMap();
            overlay.zoomToSpan();
            /**
             * 修改比例尺
             */
            MapStatus mMapStatus = new MapStatus.Builder()
                    .zoom(15)
                    .build();
            MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
            baiduMap.animateMapStatus(mMapStatusUpdate);
        }
    }

    @Override
    public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {

    }

    @Override
    public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

    }

    @Override
    public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

    }

    @Override
    public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

    }

    @Override
    public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_daohang:
                Utils.mapWalkingDaoHang(this, stNode.getLocation().latitude + "", stNode.getLocation().longitude + "", "startAddress"
                        , enNode.getLocation().latitude + "", enNode.getLocation().longitude + "", "endAddress",stNode.getCity());
                break;

        }
    }
}

3.移动app跳转到百度地图

API参考链接:http://lbsyun.baidu.com/index.php?title=uri/api/android

4.GitHub代码参考地址:https://github.com/Asmewill/BaiduMapsApiASDemos

5.百度定位demo地址:https://github.com/Asmewill/BaiduLocDemo

相关文章

网友评论

      本文标题:百度地图学习笔记

      本文链接:https://www.haomeiwen.com/subject/jincmxtx.html