1.加入权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 用于申请调用A-GPS模块 -->
2.检查权限,动态申请权限
if (ContextCompat.checkSelfPermission(LocationActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(LocationActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
getLocationLL();
} else {
ActivityCompat.requestPermissions(LocationActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
}
//回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
getLocationLL();
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)
&&!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
//弹出框 出设置界面
IOSConfirm.Builder builder = new IOSConfirm.Builder(this);
builder.setMessage(getResources().getString(R.string.no_permission));
builder.setPositiveButton(getResources().getString(R.string.dialog_ok), (dialog, which) -> {
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", getPackageName(), null));
startActivity(localIntent);
finish();
dialog.cancel();
});
builder.setNegativeButton(getResources().getString(R.string.dialog_cancel), (dialog, which) -> {
dialog.cancel();
finish();
});
IOSConfirm dialog = builder.createConfirm();
dialog.show();
} else {
finish();
}
}
}
3.获取经纬度
private void getLocationLL() {
Location location = getLastKnownLocation();
if (location != null) {
String locationStr = "纬度:" + location.getLatitude() + "\n"
+ "经度:" + location.getLongitude();
runOnUiThread(new Runnable() {
@Override
public void run() {
String method = "javascript:gpsResult('" + locationStr + "')";
webView.loadUrl(method);
}
});
LogUtils.d("经纬度:" + locationStr);
this.finish();
} else {
Toast.makeText(Config.appContext, "位置信息获取失败", Toast.LENGTH_SHORT).show();
}
}
//得到位置对象
private Location getLastKnownLocation() {
//获取地理位置管理器
LocationManager mLocationManager = (LocationManager) Config.appContext.getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
@SuppressLint("MissingPermission") Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
}
}
return bestLocation;
}
获取国家的方法,有需要可以加上
public String getAddress(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
}
sb.append(address.getLocality()).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getCountryName());
addressStr = sb.toString();
return addressStr;
}
} catch (IOException e) {
e.printStackTrace();
}
return "获取失败";
}