如何在safari浏览器中获取当前位置(城市、州、邮政编码)

How to get current position(city,state,zipcode) in safari browser ?

本文关键字:城市 位置 邮政编码 safari 浏览器 获取      更新时间:2023-09-26

我尝试过以下代码,它适用于chrom、firefox浏览器,但不适用于桌面上的safari浏览器(有线连接),也不适用于windows手机(在诺基亚lumia 625上测试),但适用于我的ipad、iphone(通过wifi)的safari浏览器
请给我一个解决这个问题的办法。

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"> </script>

在加载页面上的头调用函数中。

$(document).ready(function() {
    requestPosition();
});

在js文件中写入函数

function requestPosition() {
    if (typeof navigator.geolocation == "undefined") {
        alert("Your browser doesn't support the Geolocation API");
        return;
    }
    navigator.geolocation.getCurrentPosition(function(position){
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        "location": new google.maps.LatLng(position.coords.latitude,position.coords.longitude)
    },
    function (results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
            j = 0;
            for (var i = 0; i < results[j].address_components.length; i++) {
              if (results[j].address_components[i].types[0] == "locality") {
                  //this is the object you are looking for
                 city = results[j].address_components[i];
             }
             if (results[j].address_components[i].types[0] == "administrative_area_level_1") {
                //this is the object you are looking for
                region = results[j].address_components[i];
            }
            if (results[j].address_components[i].types[0] == "country") {
                //this is the object you are looking for
                country = results[j].address_components[i];
            }
        }
            city=city.long_name;
            state=region.long_name;
            country=country.long_name;
            $("#_state").val(state);
            $("#_country").val(country);
            $("#_city").val(city);
        }
        else
                        alert("Unable to retrieve your address");
    });
},
   function (positionError) {
    alert("Error: " + positionError.message);
   },
   {
    enableHighAccuracy: true,
    timeout: 10 * 1000 // 10 seconds
   }
}

Safari地理定位仅在连接wifi时有效。当连接有线连接时,Safari会从地理定位函数中调用错误回调。