geoLocation.GetCurrentPosition always fails in IE

2019-06-15 00:33发布

The script below works fine in FireFox and Chrome, but in Internet Explorer 11, it always fails (with POSITION_UNAVAILABLE).

I have set the browser to allow requests for position, and I agree to the prompt the browser presents me when requesting permission.

I'm almost certain that this worked fine a few months ago when I was last experimenting with it. What could I be missing as far as IE's settings?

<script type="text/javascript">
    $(document).ready(function () {
        if (Modernizr.geolocation)
        {
            navigator.geolocation.getCurrentPosition(positionSuccess, positionError, { enableHighAccuracy: true, maximumAge: 60000, timeout: 10000 })
        }
        else
        {
            $("#GeoError").html("Unable to retrieve current position.")
        }
    });

    function positionSuccess(position)
    {
        $("#Latitude").val(position.coords.latitude);
        $("#Longitude").val(position.coords.longitude);
    }

    function positionError(error)
    {
        var message = "";

        // Check for known errors
        switch (error.code) {
            case error.PERMISSION_DENIED:
                message = "This website does not have your permission to use the Geolocation API";
                break;
            case error.POSITION_UNAVAILABLE:
                message = "Your current position could not be determined.";
                break;
            case error.PERMISSION_DENIED_TIMEOUT:
                message = "Your current position could not be determined within the specified timeout period.";
                break;
        }

        // If it's an unknown error, build a message that includes 
        // information that helps identify the situation, so that 
        // the error handler can be updated.
        if (message == "") {
            var strErrorCode = error.code.toString();
            message = "Your position could not be determined due to " +
                      "an unknown error (Code: " + strErrorCode + ").";
        }

        $("#GeoError").html(message)
    }
</script>

Also, I get the same failure in IE11 when I try http://html5demos.com/geo, where both FireFox and Chrome work fine.

4条回答
地球回转人心会变
2楼-- · 2019-06-15 01:05

I have had the same issue in IE11 only. I had to set enableHighAccuracy to false to get it to work. Once I did that IE worked as expected.

查看更多
神经病院院长
3楼-- · 2019-06-15 01:06

In Internet Options, click on the Privacy tab. Uncheck the Never allow websites to request your physical location box, hit on OK.

After making these changes, the http://html5demos.com/geo now worked for me. Initially it didn't.

查看更多
贼婆χ
4楼-- · 2019-06-15 01:19

Does you enable the Location Service?

I have had the same issue, it worked after I enabled the Location Service in my windows 2016.

This page shows how to enable the Location Service in windows 10.

查看更多
▲ chillily
5楼-- · 2019-06-15 01:26

aha, just discovered something. Chrome apparently uses the WIFI access points to help finding the location.

IE11 (and edge) just falls back to the default location in your windows settings, if there is no immediate GPS signal.

查看更多
登录 后发表回答