How to get user zipcode using AngularJS

I need to fill in some data based on the zipcode user who visited the site.

Can someone tell me how to get the zipcode location of this user?

I am using AngularJS in my application.

+6
source share
2 answers

OK It's a bit tied up, but here's how I would do it if I were you.

First you used the geolocation API as follows:

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

Inside your callback, you will get a position object. It looks like this:

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

You can then take the latitude and longitude and call your server to translate it into a zip code. Getting lat / long is the hard part. Doing the math to turn it into a zip code is easy.

An alternative to calling your own server to translate lat / long to zip, you can call the Google Maps reverse search API. You give him lat for a long time, and he gives you an address full of ZIP. See HERE on how to do this.

DISCLAIMER: This will not work in IE8 because the geolocation API was not introduced before IE9. It will work in all other browsers (besides Opera Mini, #NBD).

HERE A FULL WORKING EXAMPLE I just tried this and he found my 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); }); }) 
+13
source

It seems that there is no built-in method to determine this, you will have to use the map service or the zipcode database.

0
source

Source: https://habr.com/ru/post/973943/


All Articles