Skip to content

Instantly share code, notes, and snippets.

@atheiman
Created November 24, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atheiman/a5e5c309e9aee9ee840d to your computer and use it in GitHub Desktop.
Save atheiman/a5e5c309e9aee9ee840d to your computer and use it in GitHub Desktop.
Get a client zip code using google maps api
window.navigator.geolocation.getCurrentPosition(function(pos){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var regex = /(\d{5})",\s*.*postal_code/;
var matches = regex.exec(xhttp.responseText);
console.log('ZIP code:', matches[1]);
}
};
var url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' +
pos.coords.latitude + ',' + pos.coords.longitude+'&sensor=true';
xhttp.open("GET", url, true);
xhttp.send();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment