admin管理员组文章数量:1193557
I am using the following code for getting location and city, but can't get it to give me the zip code if the person gives me permission. Please let me know if this is possible to do with this code. All it is doing is populating a text field if given permission to access.
<script type="text/javascript">
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$("#location").val(position.address.city+", "+position.address.region);
});
}
I am using the following code for getting location and city, but can't get it to give me the zip code if the person gives me permission. Please let me know if this is possible to do with this code. All it is doing is populating a text field if given permission to access.
<script type="text/javascript">
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$("#location").val(position.address.city+", "+position.address.region);
});
}
Share
Improve this question
edited Feb 20, 2017 at 20:33
Coder
2,2391 gold badge19 silver badges21 bronze badges
asked May 17, 2011 at 15:50
pertrai1pertrai1
4,31813 gold badges48 silver badges74 bronze badges
0
3 Answers
Reset to default 10Try position.address.postalCode
See the following demo i made to see what is supported by your browser/device
https://jsfiddle.net/gaby/Sx5cj/
Looking at the latest HTML5 Geolocation API, i do not see the support for position.address.city
, position.address.region
or position.address.city
yet. So, i'd have to say that is not currently supported.
Update:
Address look up seems to be supported in firefox per @Gaby aka G. Petrioli answer
There is no support for address in HTML 5 Geolocation API, but you can achieve this combining it with Google Maps API: here is an example getting the zipCode but you can use it to get the entire 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);
}
);
});
}
本文标签: javascriptGeoLocation Zip CodeStack Overflow
版权声明:本文标题:javascript - GeoLocation Zip Code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738404969a2084989.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论