Created
February 25, 2010 17:11
-
-
Save navarr/314738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Retrieve user’s Zipcode | |
// Demo at http://sandbox.gtaero.net/zipcode.html | |
function retrieve_zip(callback) | |
{ | |
try { if(!google) { google = 0; } } catch(err) { google = 0; } // Stupid Exceptions | |
if(navigator.geolocation) // FireFox/HTML5 GeoLocation | |
{ | |
navigator.geolocation.getCurrentPosition(function(position) | |
{ | |
zip_from_latlng(position.coords.latitude,position.coords.longitude,callback); | |
}); | |
} | |
else if(google && google.gears) // Google Gears GeoLocation | |
{ | |
var geloc = google.gears.factory.create('beta.geolocation'); | |
geloc.getPermission(); | |
geloc.getCurrentPosition(function(position) | |
{ | |
zip_from_latlng(position.latitude,position.longitude,callback); | |
},function(err){}); | |
} | |
} | |
function zip_from_latlng(latitude,longitude,callback) | |
{ | |
// Setup the Script using Geonames.org's WebService | |
var script = document.createElement("script"); | |
script.src = "http://ws.geonames.org/findNearbyPostalCodesJSON?lat=" + latitude + "&lng=" + longitude + "&callback=" + callback; | |
// Run the Script | |
document.getElementsByTagName("head")[0].appendChild(script); | |
} | |
function example_callback(json) | |
{ | |
// Now we have the data! If you want to just assume it's the 'closest' zipcode, we have that below: | |
zip = json.postalCodes[0].postalCode; | |
country = json.postalCodes[0].countryCode; | |
state = json.postalCodes[0].adminName1; | |
county = json.postalCodes[0].adminName2; | |
place = json.postalCodes[0].placeName; | |
alert(zip); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Zipcode</title> | |
<script type="text/javascript" src="retrieve_zipcode.js"></script> | |
</head> | |
<body> | |
<button onclick="retrieve_zip('example_callback');">Alert Zipcode</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment