【问题标题】:Issue with geolocation in IE11IE11 中的地理定位问题
【发布时间】:2015-01-25 19:15:37
【问题描述】:

我有以下代码,当用户单击/点击位置链接时,请求用户在浏览器中允许他们当前的位置。

这在 Chrome、Safari 和 Firefox 中运行良好,但我无法在 IE11 中运行。有时它会显示浏览器通知,让用户提供他们的位置,但随后什么也没有发生。

我想知道是否有其他人在使用谷歌地图和在 IE11 中请求位置时遇到过问题,以及是否有人有解决方案?

<p id="error"></p>
<form action="/" method="post">
<a class="location-link" id="location-link" href="#"><img src="/static/images/icons/location.png" alt="Get your current location" title="Get your current location" /></a><input type="text" name="location" value="" placeholder="Find a salon" >
<input class="viewbtn3" value="Submit" type="submit"></form>
<script src="/static/js/jquery.1.9.1.js"></script>
<script src="https://maps.google.com/maps/api/js?sensor=true"></script>

<script type="text/javascript">
$(document).ready(function() {
    if (typeof navigator.geolocation == "undefined") {
        $("#error").text("Your browser doesn't support the Geolocation API");
        $(".location-instruction span").hide();
        $(".location-link").hide();
        return;
    }
    $("#location-link").click(function(event) {
        event.preventDefault();
        var addressId = this.id.substring(0, this.id.indexOf("-"));
        var thisid = $(this).attr("id");
        //console.log(thisid);
        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) {
                    $("#" + thisid).parent().find('input[name="location"]').val(results[0].formatted_address);
                    $(".choose_wrap").addClass('sucess');
                } else {
                    $("#" + thisid).parent().find("#error").html("Unable to retrieve your address<br />");
                    $(".choose_wrap").addClass('fail');
                }
            })
        }, function(positionError) {
            $("#" + thisid).parent().find("#error").html("Error: " + positionError.message + "<br />")
        }, {
            enableHighAccuracy: true,
            timeout: 10 * 1000
        })
    });
});
</script>

【问题讨论】:

  • 它和其他浏览器一样工作。没问题
  • 嗯,你有没有尝试过IE11和Windows 10?
  • 是的,我尝试过 IE11,但它只有一个边距设置问题。地理位置没有问题

标签: javascript google-maps internet-explorer geolocation


【解决方案1】:

当我在 Codepen 上试用时,这实际上在 IE11 上对我有用,没有任何变化:

$(document).ready(function() {
    if (typeof navigator.geolocation == "undefined") {
        $("#error").text("Your browser doesn't support the Geolocation API");
        $(".location-instruction span").hide();
        $(".location-link").hide();
        return;
    }
    $("#location-link").click(function(event) {
        event.preventDefault();
        var addressId = this.id.substring(0, this.id.indexOf("-"));
        var thisid = $(this).attr("id");
        //console.log(thisid);
        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) {
                    $("#" + thisid).parent().find('input[name="location"]').val(results[0].formatted_address);
                    $(".choose_wrap").addClass('sucess');
                } else {
                    $("#" + thisid).parent().find("#error").html("Unable to retrieve your address<br />");
                    $(".choose_wrap").addClass('fail');
                }
            })
        }, function(positionError) {
            $("#" + thisid).parent().find("#error").html("Error: " + positionError.message + "<br />")
        }, {
            enableHighAccuracy: true,
            timeout: 10 * 1000
        })
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maps.google.com/maps/api/js?sensor=true"></script>
<p id="error"></p>
<form action="/" method="post">
  <a class="location-link" id="location-link" href="#"><img src="/static/images/icons/location.png" alt="Get your current location" title="Get your current location" /></a>
  <input type="text" name="location" value="" placeholder="Find a salon">
  <input class="viewbtn3" value="Submit" type="submit">
</form>

我唯一能建议的就是检查开发者控制台,看看你在测试时是否有任何错误。

【讨论】:

  • 非常感谢您检查 Eric。我们的客户有几个人进行了测试,但似乎对他们不起作用,但我会用 browserstack 重新检查该链接。
  • 您是否碰巧使用过 Windows 10?在 Win10 和 IE11 中,浏览器请求许可但什么也不做。
  • 不,我目前使用的是 Windows 8.1。在 Windows 10 中,我会测试 Edge 而不是 IE11。也许在您给予许可后,请尝试重新加载并再次执行。
【解决方案2】:

我在 Windows 8 上尝试过。如果关闭了机器上的位置设置(请参阅控制面板/更改位置设置),则会返回错误消息。在上面的代码中“什么都没有发生”。 “#error”和“#location-link”不共享同一个父级,因此该行不显示错误消息:

$("#" + thisid).parent().find("#error").html("Error: " + positionError.message + "<br />")

此问题是否会阻止“地理位置未打开”消息显示在客户端计算机上?

【讨论】:

  • 嘿brenzy,如果没有显示错误消息,那肯定是个问题,所以我需要修复它。只是不明白为什么IE11/Win10上有些用户可以使用位置搜索而有些不能。但我会先尝试修复该错误。
  • @doubleplusgood,原因可能是他们关闭了位置设置,但他们不知道,或者微软不支持他们的位置(请参阅其他关于为什么位置的答案不同)。希望修复错误消息会给您一个关于正在发生的事情的答案。
【解决方案3】:

这可能是 Windows 设置,至少在我的情况下是这样。尝试转到“设置”(从 Windows 的“开始”菜单中搜索)>“隐私”>“位置”并打开“位置 = 开启”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多