Skip to content

Instantly share code, notes, and snippets.

@adamgibbons
Created March 19, 2016 21:55
Show Gist options
  • Save adamgibbons/f72cb712fac7554b8d37 to your computer and use it in GitHub Desktop.
Save adamgibbons/f72cb712fac7554b8d37 to your computer and use it in GitHub Desktop.
get client's zip code (first five)
window.navigator.geolocation.getCurrentPosition(function(pos){
$.get('//maps.googleapis.com/maps/api/geocode/json?latlng='+pos.coords.latitude+','+pos.coords.longitude+'&sensor=true')
.then(function(res){
var postalCode = res.results.filter(function (result) {
return result.types.indexOf('postal_code') !== -1;
})[0];
var formattedPostalCode = postalCode.address_components.filter(function(component) {
return component.types.indexOf('postal_code') !== -1;
}).map(function(postalCode) {
return postalCode.long_name;
})[0];
return formattedPostalCode;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment