GeoLocation Postcode

I use the following code to get the location and city, but I can’t get it to give me the zip code if the person gives me permission. Please let me know if this is possible with this code. All he does is populate the text box if he is allowed access.

<script type="text/javascript"> if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { $("#location").val(position.address.city+", "+position.address.region); }); } 

+4
source share
3 answers

Try position.address.postalCode

See the following demo I did to find out what is supported by your browser / device.

http://jsfiddle.net/gaby/Sx5cj/

+7
source

Looking at the latest HTML5 Geolocation API , I do not see support for position.address.city , position.address.region or position.address.city yet. So, I have to say that it is not currently supported.


Update:

Address search seems to be supported in firefox on @Gaby aka G. Petrioli answer

+7
source

There is no address support in the HTML 5 Geolocation API, but you can achieve this by combining it with the Google Maps API: here is an example of getting zipCode, but you can use it to get the whole address:

 if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { var lat = position.coords.latitude; var long = position.coords.longitude; var point = new google.maps.LatLng(lat, long); new google.maps.Geocoder().geocode( {'latLng': point}, function (res, status) { var zip = res[0].formatted_address.match(/,\s\w{2}\s(\d{5})/); $("#location").val(zip); } ); }); } 
+1
source

All Articles