今天实现了Android Studio的高德地图APK定位
package com.example.math.www_user; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import android.widget.Toast; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import com.example.math.R; import pub.devrel.easypermissions.AfterPermissionGranted; import pub.devrel.easypermissions.EasyPermissions; public class Local_Activity extends AppCompatActivity implements AMapLocationListener { //内容 private TextView tvContent; //请求权限码 private static final int REQUEST_PERMISSIONS = 9527; //声明AMapLocationClient类对象 public AMapLocationClient mLocationClient = null; //声明AMapLocationClientOption对象 public AMapLocationClientOption mLocationOption = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_local); AMapLocationClient.updatePrivacyShow(this, true, true); AMapLocationClient.updatePrivacyAgree(this, true); requestPermission(); initLocation(); tvContent = findViewById(R.id.tv_content); } //动态请求权限 @AfterPermissionGranted(REQUEST_PERMISSIONS) private void requestPermission() { String[] permissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; if (EasyPermissions.hasPermissions(this, permissions)) { Toast.makeText(this, "已获得权限,可以定位啦!", Toast.LENGTH_SHORT).show(); } else { //false 无权限 EasyPermissions.requestPermissions(this, "需要权限", REQUEST_PERMISSIONS, permissions); } } //请求权限 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); //设置权限请求结果 EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } //初始化定位 private void initLocation() { try { mLocationClient = new AMapLocationClient(getApplicationContext()); } catch (Exception ignored) { } //设置定位回调监听 mLocationClient.setLocationListener(this); //初始化AMapLocationClientOption对象 mLocationOption = new AMapLocationClientOption(); //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //获取最近3s内精度最高的一次定位结果: //设置setOnceLocationLatest(boolean b)接口为true,启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true,setOnceLocation(boolean b)接口也会被设置为true,反之不会,默认为false。 mLocationOption.setOnceLocationLatest(true); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置定位请求超时时间,单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。 mLocationOption.setHttpTimeOut(20000); //关闭缓存机制,高精度定位会产生缓存。 mLocationOption.setLocationCacheEnable(false); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); } // 接收异步返回的定位结果 @Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { //地址 String address = aMapLocation.getAddress(); tvContent.setText(address == null ? "无地址" : address); } else { //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + aMapLocation.getErrorCode() + ", errInfo:" + aMapLocation.getErrorInfo()); } } } }
其中新增配置
<!--用于访问网络,网络定位需要上网--> <uses-permission android:name="android.permission.INTERNET" /> <!--用于读取手机当前的状态--> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!--用于写入缓存数据到扩展存储卡--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!--用于申请调用A-GPS模块--> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <!--用于进行网络定位--> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!--用于访问GPS定位--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!--用于获取运营商信息,用于支持提供运营商信息相关的接口--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!--用于访问wifi网络信息,wifi信息会用于进行网络定位--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!--用于获取wifi的获取权限,wifi信息会用来进行网络定位--> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<meta-data android:name="com.amap.api.v2.apikey" android:value="你申请的key" /> <service android:name="com.amap.api.location.APSService" />
需要注意的时,在使用地图前注意申请权限,否则会因为隐私问题而不能使用
标签:定位,04,08,mLocationOption,AMapLocationClientOption,2023,import,true,permissions From: https://www.cnblogs.com/JJTyyds/p/17299475.html