Created
November 24, 2015 19:29
-
-
Save atheiman/a5e5c309e9aee9ee840d to your computer and use it in GitHub Desktop.
Get a client zip code using google maps api
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
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