高德地图 SDK集成 定位 地理编码 搜索 经纬度获取 功能工具类

最近项目要集成高德地图,然后可以根据语音输入指令,进行定位和地理编码搜索功能,从而实现获取当前位置经纬度和根据地址获取经纬度的功能

这里写图片描述

下载文件之只放了libs和主要代码文件

https://download.csdn.net/download/qq_38355313/10352613

第一步 首先,得集成高德地图:(可看木子的高德地图集成)

https://blog.csdn.net/qq_38355313/article/details/79951300

第二步 封装

1.初始化参数

   /**
     * 初始化定位
     *
     * @author lzx
     * @since 2018年4月11日 15:38:31
     */

    private void initLocation() {
        //初始化client
        locationClient = new AMapLocationClient(context);
        locationOption = getDefaultOption();
        //设置定位参数
        geocoderSearch = new GeocodeSearch(context);
        locationClient.setLocationOption(locationOption);
        // 设置定位监听
        locationClient.setLocationListener(locationListener);
        //初始化搜索相关
        geocoderSearch.setOnGeocodeSearchListener(geocodeSearchListener);
        progDialog = new ProgressDialog(context);
    }
    ****************************************************************
     /**
     * 默认的定位参数
     *
     * @author hongming.wang
     * @since 2.8.0
     */
    private AMapLocationClientOption getDefaultOption() {
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选,设置定位模式,可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(true);//可选,设置是否gps优先,只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选,设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(3000);//可选,设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选,设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(true);//可选,设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(true);//可选,设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为true
        return mOption;
    }

2.设置定位、地理编码回调(处理结果)

 /**
     * 定位监听
     */
    AMapLocationListener locationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation location) {
            if (null != location) {
                StringBuffer sb = new StringBuffer();
                //errCode等于0代表定位成功,其他的为定位失败,具体的可以参照官网定位错误码说明
                if (location.getErrorCode() == 0) {
                    sb.append("定位成功" + "\n");
                    sb.append("定位类型: " + location.getLocationType() + "\n");
                    sb.append("经    度    : " + location.getLongitude() + "\n");
                    sb.append("纬    度    : " + location.getLatitude() + "\n");
                    sb.append("精    度    : " + location.getAccuracy() + "米" + "\n");
                    sb.append("提供者    : " + location.getProvider() + "\n");
                    sb.append("速    度    : " + location.getSpeed() + "米/秒" + "\n");
                    sb.append("角    度    : " + location.getBearing() + "\n");
                    // 获取当前提供定位服务的卫星个数
                    sb.append("星    数    : " + location.getSatellites() + "\n");
                    sb.append("国    家    : " + location.getCountry() + "\n");
                    sb.append("省            : " + location.getProvince() + "\n");
                    sb.append("市            : " + location.getCity() + "\n");
                    sb.append("城市编码 : " + location.getCityCode() + "\n");
                    sb.append("区            : " + location.getDistrict() + "\n");
                    sb.append("区域 码   : " + location.getAdCode() + "\n");
                    sb.append("地    址    : " + location.getAddress() + "\n");
                    sb.append("兴趣点    : " + location.getPoiName() + "\n");
                    //定位完成的时间
                    sb.append("定位时间: " + Utils.formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss") + "\n");
                    //返回结果
                    if (locationLatlng != null) {
                        locationLatlng.locatinmLatlng(new LatLng(location.getLatitude(), location.getLongitude()), location.getAddress());
                    }
                } else {
                    //定位失败
                    sb.append("定位失败" + "\n");
                    sb.append("错误码:" + location.getErrorCode() + "\n");
                    sb.append("错误信息:" + location.getErrorInfo() + "\n");
                    sb.append("错误描述:" + location.getLocationDetail() + "\n");
                }
                sb.append("***定位质量报告***").append("\n");
                sb.append("* WIFI开关:").append(location.getLocationQualityReport().isWifiAble() ? "开启" : "关闭").append("\n");
                sb.append("* GPS状态:").append(getGPSStatusString(location.getLocationQualityReport().getGPSStatus())).append("\n");
                sb.append("* GPS星数:").append(location.getLocationQualityReport().getGPSSatellites()).append("\n");
                sb.append("****************").append("\n");
                //定位之后的回调时间
                sb.append("回调时间: " + Utils.formatUTC(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss") + "\n");
                //解析定位结果,
                String result = sb.toString();
                Log.e("LocationServer", result);
            } else {

            }
        }
    };
*******************************************************
/**
     * 地址编译监听
     */
    GeocodeSearch.OnGeocodeSearchListener geocodeSearchListener = new GeocodeSearch.OnGeocodeSearchListener() {
        @Override
        public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

        }

        @Override
        public void onGeocodeSearched(GeocodeResult result, int rCode) {
            dismissDialog();
            if (rCode == AMapException.CODE_AMAP_SUCCESS) {
                if (result != null && result.getGeocodeAddressList() != null
                        && result.getGeocodeAddressList().size() > 0) {
                    GeocodeAddress address = result.getGeocodeAddressList().get(0);
                    //TODO 回调结果
                    locationLatlng.searchResult(new LatLng(address.getLatLonPoint().getLatitude(), address.getLatLonPoint().getLongitude()));
                } else {
                    //TODO 无结果
                }
            } else {
                //错误码
            }
        }
    };

3.编写接口回调结果

   public interface LocationLatlng {
           //返回定位结果
        void locatinmLatlng(LatLng latLng, String address);

        void searchResult(LatLng latLng);
    }
        //返回搜索结果
    public void setLocationLatlng(LocationLatlng locationLatlng) {
        this.locationLatlng = locationLatlng;
    }

    private LocationLatlng locationLatlng;
    **********************************************************
    //回调
if (locationLatlng != null) {
                        locationLatlng.locatinmLatlng(new LatLng(location.getLatitude(), location.getLongitude()), location.getAddress());
                    }
 //回调
 locationLatlng.searchResult(new LatLng(address.getLatLonPoint().getLatitude(), address.getLatLonPoint().getLongitude()));

4.activity中使用

    //地图相关工具
    private AMapUtil locationUtil;
    locationUtil = new AMapUtil(this);
        //设置监听处理回调结果
        locationUtil.setLocationLatlng(new AMapUtil.LocationLatlng() {
            @Override
            public void locatinmLatlng(LatLng latLng, String address) {
                //定位结果的回调
                mLocationFragment.moveToPosition(latLng);
                Log.d("nxm", "locatinmLatlng: " + latLng.latitude + ":" + latLng.longitude);
                mStartLatlng_latitude = latLng.latitude;
                mStartLatlng_longitude = latLng.longitude;
            }

            @Override
            public void searchResult(LatLng latLng) {
                //地理编码的回调结果
                mLocationFragment.moveToPosition(latLng);
                mEndLatlng_latitude = latLng.latitude;
                mEndLatlng_longitude = latLng.longitude;
                Log.d("nxm", "locatinmLatlng: " + latLng.latitude + ":" + latLng.longitude);
            }
        });
*****************************************
发布了43 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_38355313/article/details/79957137