It is the same when you use these prepositions for locations.
"The rule" is kind of vague and has lots of exceptions, but in general it is as follows:
- "в" for "closed" spaces like buildings....