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 latitude:Number
var longitude:Number
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 )
{
// equatorial
latitude = 0
}
else if ( northernWinterOffset > northernSummerOffset )
{
// northern hemisphere
latitude = 45
}
else
{
// southern hemisphere
latitude = -45
}
// and the estimates are...
trace( "Longitude: " + longitude ) trace( "Latitude: " + latitude )
Last year I wrote a series of posts that looked Inside the AS3 Date class. One of the gotchas I mentioned was the confusingly named .timezoneOffset
property which is actually comprised of two offsets: timezone designation (TZD) and Daylight Saving Time (DST). In the article I showed some code that split these values back out again. Well it struck me the other day that you can use these values to approximate a users position on the earth. The cool thing about this hack is that it relies solely on understanding the mechanics of timezones and daylight saving, so it will work just as well in Javascript as it does in AS2 or AS3.
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.
Caveat Emptor
So there you go, GeoLocation using just the Date class. I'm the first to admit that determining latitude is really inaccurate so if you need to know for sure then use server side IP Location. If accuracy is less important then this method is small, self contained and works in AS3, AS2 and Javascript. One last piece of trivia before I finish up; although I said employing Daylight Saving Time near the poles is largely pointless, it turns out that for reasons of synchronicity most Antarctic survey stations follow the DST rules for their home country.