javascript - How to get the zipcode of a user using AngularJS -


i need populate data based on zipcode of user visiting site.

could tell me how retrieve zipcode of location of user?

i using angularjs on app.

ok. bit involved, here how it, if you.

first, use geolocation api follows:

window.navigator.geolocation.getcurrentposition(function(pos){     console.log(pos); }); 

inside callback, position object. looks this:

{   "timestamp":1408324851386,   "coords"{     "speed":null,     "heading":null,     "altitudeaccuracy":null,     "accuracy":30,     "altitude":null,     "longitude":-111.8942634,     "latitude":40.7288257   } }  

then, can take latitude , longitude , call server translate zip code. getting lat/long hard part. doing math turn zip easy.

an alternative calling own server translate lat/long zip, call google maps' reverse lookup api. give lat long, , gives address, complete zip. see here how that.

disclaimer: won't work in ie8, geolocation api wasn't introduced until ie9. work in other browsers (besides opera mini, #nbd).

here full working example tried out, , found house, no problem.

window.navigator.geolocation.getcurrentposition(function(pos){   console.log(pos);   $http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng='+pos.coords.latitude+','+pos.coords.longitude+'&sensor=true').then(function(res){     console.log(res.data);   }); }) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -