Javascript 地理位置邮政编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6033561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GeoLocation Zip Code
提问by pertrai1
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);
});
}
回答by Gabriele Petrioli
Try position.address.postalCode
尝试 position.address.postalCode
See the following demo i made to see what is supported by your browser/device
请参阅我制作的以下演示以了解您的浏览器/设备支持的内容
回答by u476945
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.
查看最新的HTML5 地理定位 API,我还没有看到对position.address.city
、position.address.region
或的支持position.address.city
。所以,我不得不说目前不支持。
Update:
更新:
Address look up seems to be supported in firefox per @Gaby aka G. Petrioli answer
根据@Gaby aka G. Petrioli 的回答,firefox 似乎支持地址查找
回答by hectordetroya84
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:
HTML 5 Geolocation API 中不支持地址,但您可以将其与 Google Maps API 结合使用来实现:这里是获取 zipCode 的示例,但您可以使用它来获取整个地址:
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);
}
);
});
}