高德地图逆编码和正编码

2018-12-04  本文已影响25人  我不想说

/**

* 通过经纬度获取当前地址详细信息,逆地址编码

*

* @param latitude

* @param longitude

*/

getAddressInfoByLatLong(cameraPosition.target.latitude, cameraPosition.target.longitude); //传入经纬度

private void getAddressInfoByLatLong(double latitude, double longitude) {

    GeocodeSearch geocodeSearch = new GeocodeSearch(this);

    /*

    point - 要进行逆地理编码的地理坐标点。

    radius - 查找范围。默认值为1000,取值范围1-3000,单位米。

    latLonType - 输入参数坐标类型。包含GPS坐标和高德坐标。 可以参考RegeocodeQuery.setLatLonType(String)

    */

    RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(latitude, longitude), 3000, GeocodeSearch.AMAP);

    geocodeSearch.getFromLocationAsyn(query);

    geocodeSearch.setOnGeocodeSearchListener(mOnGeocodeSearchListener);

}

/**

* 响应地理编码

*/

getLatlon("河南省郑州市中原区");  //传入详情地址

public void getLatlon(final String name) {

GeocodeSearch geocoderSearch =new GeocodeSearch(this);

    GeocodeQuery query =new GeocodeQuery(name, "");// 第一个参数表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode,

    geocoderSearch.getFromLocationNameAsyn(query);// 设置同步地理编码请求

    geocoderSearch.setOnGeocodeSearchListener(mOnGeocodeSearchListener);

}

private void initListener() {  //初始化

//逆地址搜索监听器

        mOnGeocodeSearchListener =new GeocodeSearch.OnGeocodeSearchListener() {

@Override

            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

if (i ==1000) {

if (regeocodeResult !=null) {

Logger.e("ss", "  ____" + regeocodeResult.getRegeocodeAddress().getFormatAddress());

                        Logger.e("ss", regeocodeResult.getRegeocodeAddress().getProvince() +"  " + regeocodeResult.getRegeocodeAddress().getCity() +"" +"  " + regeocodeResult.getRegeocodeAddress().getDistrict());

                        tv_address.setText(regeocodeResult.getRegeocodeAddress().getProvince() + regeocodeResult.getRegeocodeAddress().getCity() + regeocodeResult.getRegeocodeAddress().getDistrict());

                    }

}

}

//正地理编码

            @Override

            public void onGeocodeSearched(GeocodeResult result, int rCode) {

if (rCode == AMapException.CODE_AMAP_SUCCESS) {

if (result !=null && result.getGeocodeAddressList() !=null

                            && result.getGeocodeAddressList().size() >0) {

GeocodeAddress address = result.getGeocodeAddressList().get(0);

                        if (address !=null) {

aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(

convertToLatLng(address.getLatLonPoint()), 15));

                            geoMarker.setPosition(convertToLatLng(address

.getLatLonPoint()));

                            addressName ="经纬度值:" + address.getLatLonPoint() +"\n位置描述:"

                                    + address.getFormatAddress();

                            ToastUtil.show(ShareLocationActivity1.this, addressName);

                        }

}else {

ToastUtil.show(ShareLocationActivity1.this, R.string.no_result);

                    }

}else {

//                    ToastUtil.showerror(this, rCode);

                }

}

};

    }

上一篇下一篇

猜你喜欢

热点阅读