Try restoring it and see if that works; if not, try opening the Compass and moving your phone around in a figure-eight motion. Apparently this recalibrates the Compass app. I'm pretty sure additional information about the compass is located on page 172 of an iPhone 3GS manual. If none of that works, try these steps (these won't help if it's a hardware issue, but would correct your GPS if the calibration was off):
1. Switch the network from 3G to EDGE (Settings > General > Network > 3G ON/OFF)
2. Open Google Maps and tap the “Location” button found on the lower left corner of the screen.
3. Wait for your location to shift and then close Google Maps.
4. Disable data services (Settings > General > Network > Cellular Data ON/OFF)
5. Perform Step 2 again and wait for your location to shift, and then close Google Maps. If it doesn't work, then you may have to repeat Step 1 and 2 multiple times.
If that doesn't help, take it to the Apple Store; they can give you a diagnosis and how much it would cost to repair. It's most likely a hardware issue.