Android高德获取逆地址编码(经纬度坐标-地址描述如省市区街道)

Android高德获取逆地址编码(经纬度坐标-地址描述如省市区街道)


可以在非地图视图下直接获取,只要传入当前位置的经纬度 当然也可以在地图模式下获取详细信息

在非第三方地图集成下(系统自带功能)获取当前位置的经纬度,参考:
http://blog.csdn.net/weixin_37577039/article/details/79178018

但是经过测试,发现这种方式获取的地理位置描述不够准确 因此还是建议通过高德地图方式进行位置获取

在高德地图集成下获取当前位置,参考:http://blog.csdn.net/weixin_37577039/article/details/79381762


获取逆地址编码

// 声明
GeocodeSearch geocoderSearch;
private void setCurrentLocationDetails(){
// 地址逆解析
 geocoderSearch = new GeocodeSearch(getApplicationContext());
 geocoderSearch.setOnGeocodeSearchListener(this);
 // 第一个参数表示一个Latlng(经纬度),第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
 RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 25, GeocodeSearch.AMAP);
 geocoderSearch.getFromLocationAsyn(query);
}

注意这里的latLonPoint不能是LatLng类型的
要为LatLonPoint类型的

定位回调监听器中调用setCurrentLocationDetails:

/**
 * 定位回调监听器
 */
public AMapLocationListener mLocationListener = new AMapLocationListener() {
    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (!IsGpsWork.isGpsEnabled(getApplicationContext())) {
            Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.hasNotOpenGps), Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        } else {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    //定位成功回调信息,设置相关消息
                    amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
                    double currentLat = amapLocation.getLatitude();//获取纬度
                    double currentLon = amapLocation.getLongitude();//获取经度
                    latLonPoint = new LatLonPoint(currentLat, currentLon);  // latlng形式的
                /*currentLatLng = new LatLng(currentLat, currentLon);*/   //latlng形式的
                    Log.i("currentLocation", "currentLat : " + currentLat + " currentLon : " + currentLon);
                    amapLocation.getAccuracy();//获取精度信息
                    setCurrentLocationDetails(); // 设置当前位置具体描述
                } else {
                    //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }
    }
};

3 获取回传数据
当然前提是 类要

implements GeocodeSearch.OnGeocodeSearchListener
    /**
     *  获取回调的逆地址内容
     */
    @Override
    public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
        String formatAddress = result.getRegeocodeAddress().getFormatAddress();
        Log.e("formatAddress", "formatAddress:"+formatAddress);
        Log.e("formatAddress", "rCode:"+rCode);
    }
    @Override
    public void onGeocodeSearched(GeocodeResult result, int rCode) {
    }

输出结果

返回结果成功或者失败的响应码。1000为成功,其他为失败


这里写图片描述


IsGpsWork类如下,用于检查GPS是否开启

public class IsGpsWork {
   //判断GPS是否开启
   public static boolean isGpsEnabled(Context context) {
       LocationManager locationManager = (LocationManager) context
               .getSystemService(Context.LOCATION_SERVICE);
       // 判断GPS模块是否开启
       return locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
   }
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过高德地图的 JavaScript API 提供的地理编码服务来获取点标记所在的省市区信息。具体步骤如下: 1. 在 Vue 组件中引入高德地图 JavaScript API: ```javascript <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> ``` 2. 初始化地图对象,并创建点标记: ```javascript mounted() { // 初始化地图对象 this.map = new AMap.Map('map-container', { zoom: 10, center: [116.397428, 39.90923] }) // 创建点标记 this.marker = new AMap.Marker({ position: [116.397428, 39.90923], map: this.map }) // 监听点标记点击事件 this.marker.on('click', this.handleMarkerClick) }, ``` 3. 在点标记点击事件处理函数中获取经纬度,并调用地理编码服务解析省市区信息: ```javascript methods: { handleMarkerClick() { // 获取点标记经纬度 const position = this.marker.getPosition() // 调用地理编码服务解析省市区信息 AMap.plugin('AMap.Geocoder', () => { const geocoder = new AMap.Geocoder({ city: '全国', radius: 1000 }) geocoder.getAddress(position, (status, result) => { if (status === 'complete' && result.regeocode) { const { province, city, district } = result.regeocode.addressComponent console.log(`省份:${province},城市:${city},区县:${district}`) } else { console.log('获取地址失败') } }) }) } } ``` 在地理编码服务的回调函数中,可以通过 `result.regeocode.addressComponent` 对象获取省市区信息。 需要注意的是,如果点标记所在的位置距离城市边界比较远,有可能会出现获取不到地址信息的情况,此时需要调整 `radius` 参数的值来扩大地理编码服务的范围。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值