1. 程式人生 > >高德地圖 SDK整合 定位 地理編碼 搜尋 經緯度獲取 功能工具類

高德地圖 SDK整合 定位 地理編碼 搜尋 經緯度獲取 功能工具類

最近專案要整合高德地圖,然後可以根據語音輸入指令,進行定位和地理編碼搜尋功能,從而實現獲取當前位置經緯度和根據地址獲取經緯度的功能

這裡寫圖片描述

下載檔案之只放了libs和主要程式碼檔案

第一步 首先,得整合高德地圖:(可看木子的高德地圖整合)

第二步 封裝

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);
            }
        });
*****************************************