Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 地理位置邮政编码_Javascript_Html_Geolocation - Fatal编程技术网

Javascript 地理位置邮政编码

Javascript 地理位置邮政编码,javascript,html,geolocation,Javascript,Html,Geolocation,我使用以下代码获取位置和城市信息,但如果对方允许,我无法获取邮政编码。请让我知道这是否可能与此代码。如果获得访问权限,它所做的只是填充一个文本字段 <script type="text/javascript"> if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { $("#location").val(position.address.c

我使用以下代码获取位置和城市信息,但如果对方允许,我无法获取邮政编码。请让我知道这是否可能与此代码。如果获得访问权限,它所做的只是填充一个文本字段

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

if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
$(“#location”).val(position.address.city+,“+position.address.region);
});
}

查看最新版本,我还没有看到对
位置.地址.城市
位置.地址.地区
位置.地址.城市
的支持。所以,我不得不说,这是目前不支持的


更新:


firefox中似乎支持地址查找,每个@Gaby aka G.Petrioli answer

尝试
position.Address.postalCode

请参阅我制作的以下演示,以了解您的浏览器/设备支持哪些功能


HTML 5地理位置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);          
            }
        );
    });
}

$(“#位置”).val(邮政编码);应该是$(“#location”).val(zip[1]);否则它将返回整个数组的内容。这主意不错,但google api是否内置于现代浏览器中?@KevinDanikowski否,您将google maps api作为CDN包含在应用程序中
(索引):78[Deprecation]getCurrentPosition()和watchPosition()不再适用于不安全的源代码。要使用这个特性,您应该考虑将应用程序切换到安全的原点,例如控制台中的http。@ AZIMUTH只使用JSFDDLE的http://版本。也更新了答案。在Chrome中不起作用<代码>位置。地址
未定义。