在百度上查了算是两天吧,什么样的方法都有,wifi定位,gps定位,等等,
首先看到的是代码及其多,各种配置,后来代码一运行各种报错,很烦,于是自己用高德sdk写了个,是公司需要,需求写完了,自己又写了个demo,先看看代码
//高德
implementation 'com.amap.api:location:latest.integration'
implementation 'com.amap.api:search:latest.integration'
//权限请求
implementation 'pub.devrel:easypermissions:1.0.1'
implementation 'com.qw:soulpermission:1.2.0'
添加权限
代码逻辑,这里我直接贴上我写的,不分开讲解了,直接放在你的项目里体会
public class MainActivity extends AppCompatActivity {
//是否定位过
private boolean isLocation = false;
private static final String[] LOCATION = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
private double startJin, startWei;
private TextView textView;
private AMapLocationClient mLocationClient;
private AMapLocationClientOption mLocationOption;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取位置服务
checkLocation();
textView = (TextView) findViewById(R.id.textView);
if (!checkGpsIsOpen()) {
//设置对话框标题
new android.app.AlertDialog.Builder(this).setTitle("提示")
//设置显示的内容
.setMessage("您需要打开定位权限,请到定位服务中开启访问我的位置信息。")
//确定按钮的响应事件
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {
//跳转到手机原生设置页面
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, 101);
//添加取消按钮
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//取消按钮的响应事件
}
//显示此对话框
}).show();
return;
} else {
}
if (!isLocation) {
checkLocation();
return;
}
}
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
isLocation = true;
Log.e("定位成功:", "纬度" + aMapLocation.getLatitude() + " 经度" + aMapLocation.getLongitude());
startJin = aMapLocation.getLongitude();
startWei = aMapLocation.getLatitude();
textView.setText("经度:" + startJin + "维度" + startWei);
Log.d("定位:", "经度" + startJin + "纬度:" + startWei);
} else {
//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
Log.d("定位", "ErrCode=" + aMapLocation.getErrorCode() + "errInfo=" + aMapLocation.getErrorInfo());
}
}
}
};
/**
* 检查定位权限 //ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION
*/
private void checkLocation() {
if (EasyPermissions.hasPermissions(MainActivity.this, LOCATION)) {
mLocationClient = new AMapLocationClient(getApplicationContext());
mLocationClient.setLocationListener(mLocationListener);
mLocationOption = new AMapLocationClientOption();
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//获取最近3s内精度最高的一次定位结果:
mLocationOption.setOnceLocationLatest(true);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
} else {
EasyPermissions.requestPermissions(this, "需要定位权限", 101, LOCATION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//是否开启gps
private boolean checkGpsIsOpen() {
boolean isOpen;
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
return isOpen;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
checkLocation();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁定位客户端,同时销毁本地定位服务。
mLocationClient.onDestroy();
}
}
代码都有注释,这里贴一下打印的信息和真机调试截图:
代码放在你的项目里肯定是可以用的,报错的话就查一下依赖和权限
感觉还是不完美,还是觉得把代码放在码云(github)里,需要的直接下载,因为我在查资料的时候,都是要积分,我自己认为是没有必要的,技术就是一起分享才能成长
帮到你就在评论区评个博主很帅,没有的话,轻轻的喷哈
源码码云地址