高德地图接入笔记

一、引入和配置

1.引入相关的sdk

    //高德地图sdk相关
    implementation 'com.amap.api:navi-3dmap:latest.integration'//导航
    implementation 'com.amap.api:search:latest.integration'//搜索
    implementation 'com.amap.api:location:latest.integration'//定位`

注意导航和3d地图implementation 'com.amap.api:3dmap:latest.integration'不能同时导入,否则报错
注意导航和2d地图implementation 'com.amap.api:map2d:latest.integration'不能同时导入,否则报错

2.创建应用,添加key
https://lbs.amap.com/dev/key/app


         
            ……


3.在AndroidManifest.xml配置权限


  

    

 

 

     

 

4.混淆

3D 地图 V5.0.0之前:
-keep   class com.amap.api.maps.**{*;} 
-keep   class com.autonavi.amap.mapcore.*{*;} 
-keep   class com.amap.api.trace.**{*;}

3D 地图 V5.0.0之后:
-keep   class com.amap.api.maps.**{*;} 
-keep   class com.autonavi.**{*;} 
-keep   class com.amap.api.trace.**{*;}

定位
-keep class com.amap.api.location.**{*;}
-keep class com.amap.api.fence.**{*;}
-keep class com.autonavi.aps.amapapi.model.**{*;}

搜索
-keep   class com.amap.api.services.**{*;}

2D地图
-keep class com.amap.api.maps2d.**{*;}
-keep class com.amap.api.mapcore2d.**{*;}

导航
-keep class com.amap.api.navi.**{*;}
-keep class com.autonavi.**{*;}

二、地图显示和定位

1.定位

    //初始化定位
    AMapLocationClient mLocationClient;
    private void locationInit() {
        //初始化定位
        mLocationClient = new AMapLocationClient(this.getActivity().getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                L.e(TAG,aMapLocation.toString());
                mBinding.tvLocation.setText(aMapLocation.getAoiName());
            }
        });
        //初始化AMapLocationClientOption对象
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景)
        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        mLocationOption.setOnceLocation(true);//获取一次定位结果
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLocationClient!=null){
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }

    }

    private void getLocationPermission(){
        AndPermission.with(this)
                .runtime()
                .permission(Permission.ACCESS_COARSE_LOCATION,Permission.ACCESS_FINE_LOCATION)
                .onGranted(permissions -> {
                    //异步查询
                   locationInit();
                })
                .onDenied(permissions -> {
                    new XPopup.Builder(this.getActivity())
                            .asConfirm("权限申请","读取视频文件,需要获取【存储】的权限,去设置开启?",
                                    "取消","去设置",
                                    () -> AppUtils.goIntentSetting(this.getActivity()),null,true).show();
                })
                .start();
    }

2.显示地图,并定位到自己位置


image.png

2.1.布局中创建

        

2.2.获取地图,并设置定位到自己当前位置

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).statusBarDarkFont(true).init();
        db.mapView.onCreate(savedInstanceState);
        initMap();
        initView();
    }

    private void initMap() {
        AMap aMap = db.mapView.getMap();
        aMap.setMyLocationEnabled(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        db.mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
        db.mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
        db.mapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
        db.mapView.onSaveInstanceState(outState);
    }

    private void initView() {
        tvTitle.setText("选择地址");

    }

更多定位蓝点展示模式参考:https://lbs.amap.com/api/android-sdk/guide/create-map/mylocation

三、poi检索的时候需要选择省份,然后通过关键字检索

    private String[] provinceList =new String[]{"北京市", "天津市",  "重庆市","上海市","河北省", "山西省",  "辽宁省", "吉林省",
"黑龙江省",  "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省","河南省", "湖北省", "湖南省", "广东省",  "海南省","四川省", 
"贵州省", "云南省",  "陕西省", "甘肃省", "青海省","台湾省","西藏自治区", "内蒙古自治区","广西壮族自治区","宁夏回族自治区", 
"新疆维吾尔自治区",  "香港特别行>政区", "澳门特别行政区"};

把上面的34省份选择一个后作为cityCode,再传入对应的关键字,就可以检索出来poi列表信息

    int currentPage=1;
    PoiSearch.Query query;
    PoiSearch poiSearch;
    private void getPoi(String keyWord){
        //keyWord表示搜索字符串,第二个参数表示POI搜索类型,cityCode表示POI搜索区域
        query = new PoiSearch.Query(keyWord, "", cityCode);
        query.setPageSize(20);
        query.setPageNum(currentPage);//设置查询页码

        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                ArrayList pois = poiResult.getPois();
                if (pois!=null&&pois.size()>0){
                    if (currentPage==1){
                        //地图移动到第一个检索POI位置
                        mAdapter.setNewData(pois);
                    }else mAdapter.addData(pois);

                    mAdapter.loadMoreComplete();
                }else mAdapter.loadMoreEnd();
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });
        poiSearch.searchPOIAsyn();
    }
image.png

四、异常相关

异常一:More than one file was found with OS independent path 'assets/location_map_gps_locked.png'

发现

image.png
里面有重复的资源图片,其实直接去掉2d地图的sdk就行了,但也可以用下面的方法解决。
解决方案:在Module:app中的android中写入

packagingOptions {
        exclude 'assets/location_map_gps_locked.png'
        exclude 'assets/location_map_gps_3d.png'
    }

异常二:javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found`

导致无法定位和检索poi
网络设置了代理,导致
https://blog.csdn.net/flyfight88/article/details/84099014

你可能感兴趣的:(高德地图接入笔记)