The demo above attempts to locate your current position on the earth. I can’t take any credit for the 3D stuff as I based it on Andy Zupko’s great post about Geolocation in Papervision3D. My little contribution is the code below. This takes the humble Date class and does some reverse engineering on it to derive your approximate location on the earth. First the code, then I’ll explain how it works…
// Estimate Latitide and Longitude from the Date class.
var now:Date = new Date()
var northernWinter:Date = new Date( 2000, 0, 1 )
var northernSummer:Date = new Date( 2000, 6, 1 )
var northernWinterOffset:Number = northernWinter.timezoneOffset
var northernSummerOffset:Number = northernSummer.timezoneOffset
// Find just the Timezone modifier TZD (in minutes).
// DST modifier is negative so TZD is the larger value.
var tzd:Number = Math.max( northernWinterOffset, northernSummerOffset )
// if you need the current value of DST (in minutes) it's...
var dst:Number = now.timezoneOffset - tzd
// Estimate Longitude:
// 60 minutes of time == 15 degrees of longitude.
longitude = tzd / -4
// Estimate Latitude:
// If DST is adopted we can detect the current season and from that we can find the hemisphere. If not assume equatorial.
if ( northernWinterOffset == northernSummerOffset )
latitude = 0
else if ( northernWinterOffset > northernSummerOffset )
// northern hemisphere
latitude = 45
// southern hemisphere
latitude = -45
// and the estimates are...
trace( "Longitude: " + longitude )
trace( "Latitude: " + latitude )
If you follow the code above you’ll see the first thing we do is split out TZD and DST. Once you have these values you can make some estimates about latitude and longitude.
TZD == Longitude
The earth rotates once on it’s axis each day so that’s 360° in 24 hours, or 15° of longitude every hour. So if we know a user’s TZD we can estimate their longitude (east/west) to within about 15°. In reality timezone boundaries are a little wobbly so I’d gave or take another 15°.
DST == Latitude
Using DST to estimate latitude is really inaccurate but if you need to know then this way is better than nothing – here’s the theory anyway. As a general rule Daylight Saving Time is adopted by countries with temperate climates. Employing it nearer the poles is pointless and the last thing you need at the equator is more sunshine. In the code above we created a summer and a winter date. By comparing these we can tell if the user’s current location employs DST and if it is currently in effect.
There are plenty of special cases for DST (don’t get me started on that) but if no DST offset exists in summer or winter then the user is probably within the equatorial band. If DST exists but is not in effect then that hemisphere is in winter, and the opposite hemisphere is in summer. A quick check against the current date will tell you which hemisphere that temporal band is in.