1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android高德地图获取当前地理位置(不显示地图只获取当前位置)

Android高德地图获取当前地理位置(不显示地图只获取当前位置)

时间:2021-09-13 21:14:32

相关推荐

Android高德地图获取当前地理位置(不显示地图只获取当前位置)

Android高德地图获取当前地理位置(不显示地图只获取当前位置)

一、集成和权限配置

参考:/weixin_37577039/article/details/79177131

二、

//声明AMapLocationClient类对象AMapLocationClient mLocationClient = null;//声明AMapLocationClientOption对象public AMapLocationClientOption mLocationOption = null;

onCreate()中

getCurrentLocationLatLng();

三、初始化定位并设置定位回调监听

/*** 初始化定位并设置定位回调监听*/private void getCurrentLocationLatLng(){//初始化定位mLocationClient = new AMapLocationClient(getApplicationContext());//设置定位回调监听mLocationClient.setLocationListener(mLocationListener);//初始化AMapLocationClientOption对象mLocationOption = new AMapLocationClientOption();/* //设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景) 设置了场景就不用配置定位模式等option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);if(null != locationClient){locationClient.setLocationOption(option);//设置场景模式后最好调用一次stop,再调用start以保证场景模式生效locationClient.stopLocation();locationClient.startLocation();}*/// 同时使用网络定位和GPS定位,优先返回最高精度的定位结果,以及对应的地址描述信息mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//只会使用网络定位/* mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);*///只使用GPS进行定位/*mLocationOption.setLocationMode(AMapLocationMode.Device_Sensors);*/// 设置为单次定位 默认为false/*mLocationOption.setOnceLocation(true);*///设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。默认连续定位 切最低时间间隔为1000msmLocationOption.setInterval(3500);//设置是否返回地址信息(默认返回地址信息)/*mLocationOption.setNeedAddress(true);*///关闭缓存机制 默认开启 ,在高精度模式和低功耗模式下进行的网络定位结果均会生成本地缓存,不区分单次定位还是连续定位。GPS定位结果不会被缓存。/*mLocationOption.setLocationCacheEnable(false);*///给定位客户端对象设置定位参数mLocationClient.setLocationOption(mLocationOption);//启动定位mLocationClient.startLocation();}

四、定位回调监听器

/*** 定位回调监听器*/public AMapLocationListener mLocationListener = new AMapLocationListener() {@Overridepublic void onLocationChanged(AMapLocation amapLocation) {if (!IsGpsWork.isGpsEnabled(getApplicationContext())) {Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.hasNotOpenGps), Toast.LENGTH_SHORT);toast.setGravity(Gravity.CENTER, 0, 0);toast.show();} else {if (amapLocation != null) {if (amapLocation.getErrorCode() == 0) {//定位成功回调信息,设置相关消息amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表double currentLat = amapLocation.getLatitude();//获取纬度double currentLon = amapLocation.getLongitude();//获取经度latLonPoint = new LatLonPoint(currentLat, currentLon); // latlng形式的/*currentLatLng = new LatLng(currentLat, currentLon);*/ //latlng形式的Log.i("currentLocation", "currentLat : " + currentLat + " currentLon : " + currentLon);amapLocation.getAccuracy();//获取精度信息} else {//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。Log.e("AmapError", "location Error, ErrCode:"+ amapLocation.getErrorCode() + ", errInfo:"+ amapLocation.getErrorInfo());}}}}};

@Overrideprotected void onDestroy() {super.onDestroy();if(mLocationClient!=null) {mLocationClient.onDestroy();//销毁定位客户端。}}@Overrideprotected void onStart() {super.onStart();if(mLocationClient!=null) {mLocationClient.startLocation(); // 启动定位}}@Overrideprotected void onPause() {super.onPause();if(mLocationClient!=null) {mLocationClient.stopLocation();//停止定位}}

获取当前位置具体描述(逆地址编码),参考:

/weixin_37577039/article/details/79177992

参考:高德地图官方文档:/api/android-location-sdk/guide/android-location/getlocation

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