I've written a code that use HTML5 Geolocation API to get user position.

The code is written for mobile website.

The code works fine, but every times that page is reloaded Safari (iOS) asks the user, through the system popup, if she wants to share her current position or not.

I want to know if there's the possibility not to ask for the user's confirmation every time, as the user has already given permission to use her position.

Here's my code:

 if (navigator.geolocation){
     navigator.geolocation.getCurrentPosition(st.onSuccess,st.onError);
 }else{
     st.onNotSupportedBrowser();
 }

These methods(st.onSuccess, st.onError, st.onNotSupportedBrowser) are the callbacks that I use when the events get triggered.

Thanks

有帮助吗?

解决方案

I confirm that isn't possible to limit the opening of geolocation authorization system popup because it is managed by Safari's core and it performs the API in this way. There're no bugs in the upper code, because I use the native HTML5 API.

Hope that this helps you.

其他提示

Set a cookie, check if the cookie exists and just return if it does exists. Then you can set a cookie to expire for however long you want and it won't ask again until that cookie is expired.

function getGeoLocation() {
    if (getCookie('cookie-name-here')) return;

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(setGeoLocation, showGeoError, {
            maximumAge: 6E4, // 6 followed by 4 zerors => 60000 milliseconds
            timeout: 5E4 // 5 followed by 4 zeros => 50000 milliseconds
        });
    } else {
        console.log('Geolocation is not supported by this browser.');
    }
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return '';
}

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = 'expires=' + d.toUTCString();
    var maxAge = 'max-age=' + exdays * 24 * 60 * 60;
    var path = 'path=/';
    document.cookie = cname + "=" + cvalue + "; " + expires + "; " + maxAge + '; ' + path;
}

The setGeoLocation and showGeoError would be application specific so I've left that out.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top