We study the valence electron of an alkaline atom or a general charged particle with arbitrary spin and with magnetic moment moving in a rotating magnetic field. By using a time-dependent unitary transformation, the Schr\"odinger equation with the time-dependent Hamiltonian can be reduced to a Schr\"odinger-like equation with a time-independent effective Hamiltonian. Eigenstates of the effective Hamiltonian correspond to cyclic solutions of the original Schr\"odinger equation. The nonadiabatic geometric phase of a cyclic solution can be expressed in terms of the expectation value of the component of the total angular momentum along the rotating axis, regardless of whether the solution is explicitly available. For the alkaline atomic electron and a strong magnetic field, the eigenvalue problem of the effective Hamiltonian is completely solved, and the geometric phase turns out to be a linear combination of two solid angles. For a weak magnetic field, the same problem is solved partly. For a general charged particle, the problem is solved approximately in a slowly rotating magnetic field, and the geometric phases are also calculated.