基于android对接百度地图搜索附近关键字列表展示并调用第三方地图应用打开导航(百度地图、高德地图)

经过两天的折腾,终于把需求实现了,记录下过程与遇到的坑详情链接
先上效果图
搜索结果
基于android对接百度地图搜索附近关键字列表展示并调用第三方地图应用打开导航(百度地图、高德地图)_第1张图片
点击列表中的去这里调取第三方地图APP,效果图
基于android对接百度地图搜索附近关键字列表展示并调用第三方地图应用打开导航(百度地图、高德地图)_第2张图片

首先配置百度地图

		bBaiduMap = mMapView.getMap();
        bBaiduMap.setMyLocationEnabled(true);
        //定位初始化
        mLocationClient = new LocationClient(getActivity());
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.zoom(17.0f);
        bBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
        //通过LocationClientOption设置LocationClient相关参数
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); //打开gps
        option.setCoorType("bd09ll"); //设置坐标类型
        option.setScanSpan(10000);
        option.setIsNeedAddress(true);
        //设置locationClientOption
        mLocationClient.setLocOption(option);

        //注册LocationListener监听器
        MyLocationListener myLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myLocationListener);
        //开启地图定位图层
        mLocationClient.start();

搜索结果数据

		poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                MyLog.e("PoiResult   " + poiResult.error);
                if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {
                    beans.clear();
                    List allPoi = poiResult.getAllPoi();
                    for (PoiInfo info : allPoi) {
                        MapBean bean = new MapBean();
                        bean.city = info.city;
//                        bean.distance = info.distance + "";
                        bean.name = info.name;
                        bean.addr = info.address;
                        bean.iv = getResources().getDrawable(R.mipmap.msg_img);
                        beans.add(bean);
                    }
                    adapter.setList(beans);
                }

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
                MyLog.e("PoiDetailResult   " + poiDetailResult.error);
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
                MyLog.e("poiDetailSearchResult   " + poiDetailSearchResult.error);
            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
                MyLog.e("poiIndoorResult   " + poiIndoorResult.error);
            }
        });

搜索事件

					poiSearch.searchInCity(new PoiCitySearchOption()
                            .city(city)
                            .keyword(v.getText().toString())//搜索的关键字
                            .pageCapacity(20)//一次搜索多少条数据
                            .pageNum(0));//0代表第1页

地址监听

public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            city = location.getCity();
            longitude = location.getLongitude();
            latitude = location.getLatitude();
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(location.getDirection()).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            bBaiduMap.setMyLocationData(locData);
            LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
            if (isFirstLocate) {
                isFirstLocate = false;
                //给地图设置状态
                bBaiduMap.animateMapStatus(MapStatusUpdateFactory.newLatLng(ll));
            }
        }
    }

点击调取第三方地图APP事件

						mSearch.geocode(new GeoCodeOption()
                                .city(m.city)//城市
                                .address(m.addr));//详细地址



		mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                LatLng location = geoCodeResult.getLocation();
                String address = geoCodeResult.getAddress();
                if (location != null) {
               
                        if (MapNaviUtils.isBaiduMapInstalled()) {//手机内是否安装了百度地图
                            MapNaviUtils.openBaiDuNavi(getActivity(), addr);

                        } else if (MapNaviUtils.isGdMapInstalled()) {//手机内是否安装了高德地图
                            MapNaviUtils.openGaoDeNavi(getActivity(), addr);

                        } else {//都没安装下载百度地图
                            new AlertDialog.Builder(getActivity())
                                    .setMessage("下载百度地图?")
                                    .setNegativeButton("取消", null)
                                    .setPositiveButton("下载", (dialog12, which12) -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(MapNaviUtils.DOWNLOAD_BAIDU_MAP))))
                                    .show();
                        }
                    
	                } else {
	                    MyLog.e("onGetGeoCodeResult  location=null ");
	                }
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
            }
        });

打开百度地图

    public static void openBaiDuNavi(Context context, String dname) {
        String uri = "baidumap://map/geocoder?src=com.construction5000.yun&address=";
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage(PN_BAIDU_MAP);
        intent.setData(Uri.parse(uri + dname));
        context.startActivity(intent);
    }

打开高德地图

    public static void openGaoDeNavi(Activity context, String dname) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setPackage(PN_GAODE_MAP);
        intent.setData(Uri.parse("androidamap://keywordNavi?sourceApplication=softname&keyword="+dname+"&style=2"));
        context.startActivity(intent);

    }

下篇遇到的坑详情链接

你可能感兴趣的:(迎刃而解,android,百度,定位,经验分享,app)