1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 高德地图 SDK集成 定位 地理编码 搜索 经纬度获取 功能工具类

高德地图 SDK集成 定位 地理编码 搜索 经纬度获取 功能工具类

时间:2021-07-24 07:55:23

相关推荐

高德地图  SDK集成 定位 地理编码 搜索 经纬度获取 功能工具类

最近项目要集成高德地图,然后可以根据语音输入指令,进行定位和地理编码搜索功能,从而实现获取当前位置经纬度和根据地址获取经纬度的功能

下载文件之只放了libs和主要代码文件

/download/qq_38355313/10352613

第一步 首先,得集成高德地图:(可看木子的高德地图集成)

/qq_38355313/article/details/79951300

第二步 封装

1.初始化参数

/*** 初始化定位** @author lzx* @since 4月11日 15:38:31*/private void initLocation() {//初始化clientlocationClient = 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);//可选,设置是否返回逆地理地址信息。默认是truemOption.setOnceLocation(true);//可选,设置是否单次定位。默认是falsemOption.setOnceLocationLatest(false);//可选,设置是否等待wifi刷新,默认为false.如果设置为true,会自动变为单次定位,持续定位时不要使用AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选, 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTPmOption.setSensorEnable(true);//可选,设置是否使用传感器。默认是falsemOption.setWifiScan(true); //可选,设置是否开启wifi扫描。默认为true,如果设置为false会同时停止主动刷新,停止以后完全依赖于系统刷新,定位位置可能存在误差mOption.setLocationCacheEnable(true); //可选,设置是否使用缓存定位,默认为truereturn mOption;}

2.设置定位、地理编码回调(处理结果)

/*** 定位监听*/AMapLocationListener locationListener = new AMapLocationListener() {@Overridepublic 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() {@Overridepublic void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {}@Overridepublic 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() {@Overridepublic 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;}@Overridepublic void searchResult(LatLng latLng) {//地理编码的回调结果mLocationFragment.moveToPosition(latLng);mEndLatlng_latitude = latLng.latitude;mEndLatlng_longitude = latLng.longitude;Log.d("nxm", "locatinmLatlng: " + latLng.latitude + ":" + latLng.longitude);}});*****************************************

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。