浏览器获取地理方位

<script language="javascript">
    var position_option = {
        enableHighAccuracy : true,
        maximumAge : 30000,
        timeout : 20000
    };
    navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError,position_option);
    function getPositionSuccess(position) {
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
        //alert("您所在的位置: 纬度" + lat + ",经度" + lng);
        document.getElementById("site_longitude").value = lng;
        document.getElementById("site_latitude").value = lat;
        //alert(position.coords);
        if (typeof position.address != "undefined") {
            var country = position.address.country;
            var province = position.address.region;
            var city = position.address.city;
            alert(' 您位于 ' + country + province + '省' + city
                    + '市');
        }
        //document.write(lat);
    }
    function getPositionError(error) {
        switch (error.code) {
        case error.TIMEOUT:
            alert("连接超时,请重试");
            break;
        case error.PERMISSION_DENIED:
            alert("您拒绝了使用位置共享服务,查询已取消");
            break;
        case error.POSITION_UNAVAILABLE:
            alert("获取位置信息失败");
            break;
        }
    }
</script>


你可能感兴趣的:(浏览器,function,position,地理)