Magnetic barrier in confined two-dimensional electron gases: Nanomagnetometers and magnetic switches