用高德地图sdk返回详细地址描述

上一次写了百度地图取得详细位置的方法,今天继续看看用高德地图怎么实现。
首先同样要申请api_key步骤与百度地图一样,需要的东西也一样。
然后下载高德地图的定位sdk和服务sdk。
接下来步骤都和百度地图操作步骤类似:导入jar包,配置api_key。
开始代码:
在活动中实现AMapLocationListener以取得高德返回的经纬度
在活动中实现OnGeocodeSearchListener以取得高德返回逆地理编码结果

初始化:

	/**
	 * 初始化定位
	 */
	private void init() {
		// 初始化定位
		mLocationManagerProxy = LocationManagerProxy.getInstance(getActivity());
		mLocationManagerProxy.setGpsEnable(true);
		//初始化地理编码服务
		geocoderSearch = new GeocodeSearch(getActivity());
		geocoderSearch.setOnGeocodeSearchListener(this);
		// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
		// 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用removeUpdates()方法来取消定位请求
		// 在定位结束后,在合适的生命周期调用destroy()方法 (demo中是在pause中调用)
		// 其中如果间隔时间为-1,则定位只定一次,
		//在单次定位情况下,定位无论成功与否,都无需调用removeUpdates()方法移除请求,定位sdk内部会移除
		mLocationManagerProxy.requestLocationData(
				LocationProviderProxy.AMapNetwork, 60*1000, 15,this );

	}

取得经纬度:

@Override
public void onLocationChanged(AMapLocation amapLocation) {
	// TODO Auto-generated method stub
	if (amapLocation!=null&&amapLocation.getAMapException().getErrorCode() == 0) {
		// 定位成功回调信息,设置相关消息
		mLocationLatlngTextView.setText(amapLocation.getLatitude() + "  "
				+ amapLocation.getLongitude());
		mLocationAccurancyTextView.setText(String.valueOf(amapLocation
				.getAccuracy()));
		mLocationMethodTextView.setText(amapLocation.getProvider());

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(amapLocation.getTime());
		mLocationTimeTextView.setText(df.format(date));
		
		if (amapLocation.getProvider().equalsIgnoreCase(LocationProviderProxy.AMapNetwork)) {
			//如果是网络定位则直接返回
			mLocationDesTextView.setText(amapLocation.getAddress());
			if (amapLocation.getProvince() == null) {
				mLocationProvinceTextView.setText("null");
			} else {
				mLocationProvinceTextView.setText(amapLocation.getProvince());
			}
			mLocationCityTextView.setText(amapLocation.getCity());
			mLocationCountyTextView.setText(amapLocation.getDistrict());
		} else {
			//否则取得经纬度进行逆地理编码
			LatLonPoint latLonPoint=new LatLonPoint(amapLocation.getLatitude(), amapLocation.getLongitude());
			getAddress(latLonPoint);
		}
	}
	else {
		Log.i("gaodeMap", Integer.toString(amapLocation.getAMapException().getErrorCode()));
	}
}

/**
 * 响应逆地理编码
 */
public void getAddress( LatLonPoint latLonPoint) {
	RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,
			GeocodeSearch.AMAP);// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
	geocoderSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求
}

//	regeocodeResult - 逆地理编码返回的结果。
//	rCode - 返回结果成功或者失败的响应码。0为成功,其他为失败(详细信息参见网站开发指南-错误码对照表)。
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
	// TODO Auto-generated method stub
	if (rCode == 0) {
		if (result != null && result.getRegeocodeAddress() != null
				&& result.getRegeocodeAddress().getFormatAddress() != null) {
//				String addressName = result.getRegeocodeAddress().getFormatAddress();
			mLocationDesTextView.setText(result.getRegeocodeAddress().getFormatAddress());
			
			if (result.getRegeocodeAddress().getProvince()== null) {
				mLocationProvinceTextView.setText("null");
			} else {
				mLocationProvinceTextView.setText(result.getRegeocodeAddress().getProvince());
			}
			mLocationCityTextView.setText(result.getRegeocodeAddress().getCity());
			mLocationCountyTextView.setText(result.getRegeocodeAddress().getDistrict());
			
		} else {
			mLocationDesTextView.setText("没有结果");
		}
	} else if (rCode == 27) {
		mLocationDesTextView.setText("网络错误");
	} else if (rCode == 32) {
		mLocationDesTextView.setText("key无效");
	} else {
		mLocationDesTextView.setText(rCode);
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值