项目需要集成地图 获取周边信息:
本次使用高德地图。高德注册信息这里忽略。直接记录 在集成高德地图过程中的问题。
1. debug版本测试可以不适用key 不需要SHA 。直接访问。在Release 包下面。直接提示SHA错误。。我去。什么情况。检查没有问题。
再次获取SHA。通过代码获取如下:
/** * Created by jones on 2018/6/25. * 获取应用的SHA */ public class AppSHAUtils { /*获取APP SHA 方法*/ public static String sHA1(Context context) { try { PackageInfo info = context.getPackageManager().getPackageInfo( context.getPackageName(), PackageManager.GET_SIGNATURES); byte[] cert = info.signatures[0].toByteArray(); MessageDigest md = MessageDigest.getInstance("SHA1"); byte[] publicKey = md.digest(cert); StringBuffer hexString = new StringBuffer(); for (int i = 0; i < publicKey.length; i++) { String appendString = Integer.toHexString(0xFF & publicKey[i]) .toUpperCase(Locale.US); if (appendString.length() == 1) hexString.append("0"); hexString.append(appendString); hexString.append(":"); } // String result =hexString.toString(); return hexString.toString().substring(0, hexString.toString().length() - 1); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } }
在application 中调用获取本应用的key。
测试OK。很高兴。。
搜索周边代码:
@Override public void onLocationChanged(AMapLocation amapLocation) { //第一次进入获取当前定位位置 // Logger.e(TAG, " onLocationChanged:" + amapLocation.getErrorCode() +",amapLocation.getAddress():"+amapLocation.getAddress()); if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { if (!isFirstLoc) { Logger.e(TAG, "onLocationChanged isFirstLoc:" + isFirstLoc); return; } // seach(amapLocation.getAddress()); isFirstLoc = false; //定位成功回调信息,设置相关消息 amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表 double latitude = amapLocation.getLatitude();//获取纬度 double longitude = amapLocation.getLongitude();//获取经度 amapLocation.getAccuracy();//获取精度信息\ /* AddressBean ab = new AddressBean(latitude, longitude, amapLocation.getAoiName(), amapLocation.getCity(), amapLocation.getAddress()); Logger.e(TAG, "amapLocation getAddress:" + amapLocation.getAddress() + ",amapLocation getAoiName :" + amapLocation.getAoiName() + ",amapLocation.getStreetNum()" + amapLocation.getStreetNum() + ",amapLocation.getDistrict():" + amapLocation.getDistrict() + "amapLocation getStreet:" + amapLocation.getStreet()); data.add(ab); adapter.refreshData(data);*/ adapter.notifyDataSetChanged(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(amapLocation.getTime()); df.format(date);//定位时间 PoiSearch.Query query = new PoiSearch.Query("", "", ""); query.setPageSize(100); PoiSearch search = new PoiSearch(this, query); search.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 5000)); search.setOnPoiSearchListener(this); search.searchPOIAsyn(); } else { //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。 Logger.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } } }
我这里设置了半径 5000 是5公里。
因为如果发生经纬度变化会一直获取。我用标签。只取第一次进入的时候的值。
感觉很OK了。
结果发现在退出 定位页面的时候。定位仍然启用。。这个就不是很好了。
找了一下在一下方法。
@Override protected void onDestroy() { super.onDestroy(); // 退出的时候销毁监听位置更新操作 mLocationClient.onDestroy(); // finish(); Logger.e(TAG, "onDestroy"); }可以玩咯。