当前位置:首页教育技巧word技巧word基础知识

高德地图链接怎么导入word,导入高德地图定位功能

减小字体 增大字体 2024-01-20 09:26:13


定位功能已经是很多项目中都在使用的了,而且也比较简单,只要按照第三方给出的文档进行集成就可以了,起中高德的地图个人感觉是集成比较简单的了,这个定位功能也一样。、

首先,是注册一个高德账号,这个就不用说了,然后就是创建一个项目,按照要求注册sha1值,包名等。

需要注意一点,有时候你直接用电脑小黑窗弄到的sha1值是不对的,在高德地图上会返回key值有误!

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 result.substring(0, result.length()-1); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; }

用上面的方法,传入上下文,返回值就是一个sha1值,用这个值去申请key值就可以了。

然后,在清单文件中配置高德的key值

<meta-data android:name="com.amap.api.v2.apikey" android:value="你自己的key值"> 添加权限

在添加依赖

//定位 compile 'com.amap.api:location:latest.integration'

创建Application类,记得在清单文件中注册

public class MyApp extends Application { //声明AMapLocationClientOption对象 public AMapLocationClientOption mLocationOption = null; //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; //声明定位回调监听器 public AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation aMapLocation) { } }; @Override public void onCreate() { super.onCreate(); //初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); } } 注册上 Application类

然后,就可以在需要的位置进行调用了,不过现在很多手机需要动态申请权限,所以就需要加上权限判断 //权限申请 private static final int LOCATION_CODE = 1; private LocationManager lm;//【位置管理】

public void quanxian(){ lm = (LocationManager) MainActivity.this.getSystemService(MainActivity.this.LOCATION_SERVICE); boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); if (ok) {//开了定位服务 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.e("BRG","没有权限"); // 没有权限,申请权限。 // 申请授权。 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE);// Toast.makeText(getActivity(), "没有权限", Toast.LENGTH_SHORT).show(); } else { dingwei(); // 有权限了,去放肆吧。// Toast.makeText(getActivity(), "有权限", Toast.LENGTH_SHORT).show(); } } else { Log.e("BRG","系统检测到未开启GPS定位服务"); Toast.makeText(MainActivity.this, "系统检测到未开启GPS定位服务", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(); intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent, 1315); }}

@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case LOCATION_CODE: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 权限被用户同意。 dingwei(); } else { // 权限被用户拒绝了。 Toast.makeText(MainActivity.this, "定位权限被禁止,相关地图功能无法使用!",Toast.LENGTH_LONG).show(); } Intent intent = new Intent(MainActivity.this, MainActivity.class); startActivity(intent); finish(); } } }

然后,就是在权限允许里面调用定位功能,

//声明mlocationClient对象 public AMapLocationClient mlocationClient; //声明mLocationOption对象 public AMapLocationClientOption mLocationOption = null; private void dingwei() { Log.e("BRG","走了定位"); mlocationClient = new AMapLocationClient(MainActivity.this); //初始化定位参数 mLocationOption = new AMapLocationClientOption(); //设置定位监听 mlocationClient.setLocationListener( this); //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位间隔,单位毫秒,默认为2000ms mLocationOption.setInterval(20000); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置定位参数 mlocationClient.setLocationOption(mLocationOption); // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, // 注意设置合适的定位时间的间隔(最小间隔支持为1000ms),并且在合适时间调用stopLocation()方法来取消定位请求 // 在定位结束后,在合适的生命周期调用onDestroy()方法 // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除 //启动定位 mlocationClient.startLocation(); }

重写方法,拿到地址返回值

@Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null) { Log.e("BRG","返回结果"+aMapLocation.getErrorCode()); if (aMapLocation.getErrorCode() == 0) { //可在其中解析amapLocation获取相应内容。 String address = aMapLocation.getAddress(); }else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError","location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo()); } } } 到这里,最简单的一个定位功能就完成了,根据高德给出来的返回值列表,自己去取需要的AMapLocation内部的值吧:http://lbs.amap/api/android-location-sdk/guide/android-location/getlocation

评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分)

【免责声明】本站信息来自网友投稿及网络整理,内容仅供参考,如果有错误请反馈给我们及时更正,对文中内容的真实性和完整性本站不提供任何保证,不承但任何责任。
版权所有:学窍知识网 Copyright © 2011-2024 www.at317.com All Rights Reserved .