上一次写了百度地图取得详细位置的方法,今天继续看看用高德地图怎么实现。
首先同样要申请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); } }
用高德地图sdk返回详细地址描述
最新推荐文章于 2023-03-13 17:20:44 发布