首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不适用于IOS safari

不适用于IOS safari
EN

Stack Overflow用户
提问于 2017-10-02 08:40:13
回答 1查看 3.2K关注 0票数 1

更新:它不仅在safari上工作(chrome正确工作)。

我正在尝试使用google获取用户位置。

它在桌面FF和Chrome上正确工作(显示城市和国家),但在iOS safari (iOS 11)上没有显示任何内容。而且,在其他移动设备上,它似乎不能正常工作。

请注意,我使用的是HTTPS,所以安全性没有问题。

以下是javascript代码:

代码语言:javascript
复制
function getLocation(){
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(showPosition,showError);
    }
    else{
        document.getElementById("userlocation").innerHTML="Geolocation is not supported by this browser.";
    }
}

function showPosition(position){
    lat=position.coords.latitude;
    lon=position.coords.longitude;
    displayLocation(lat,lon);
}

function showError(error){
    switch(error.code){
        case error.PERMISSION_DENIED:
            document.getElementById("userlocation").innerHTML="User denied the request for Geolocation."
        break;
        case error.POSITION_UNAVAILABLE:
            document.getElementById("userlocation").innerHTML="Location information is unavailable."
        break;
        case error.TIMEOUT:
            document.getElementById("userlocation").innerHTML="The request to get user location timed out."
        break;
        case error.UNKNOWN_ERROR:
            document.getElementById("userlocation").innerHTML="An unknown error occurred."
        break;
    }
}

function displayLocation(latitude,longitude){
    var geocoder;
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(latitude, longitude);

    geocoder.geocode(
        {'latLng': latlng}, 
        function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    var add= results[0].formatted_address ;
                    var  value=add.split(",");

                    count=value.length;
                    country=value[count-1];
                    state=value[count-2];
                    city=value[count-3];
                    document.getElementById("userlocation").innerHTML = city + ", " + country;
                }
                else  {
                    document.getElementById("userlocation").innerHTML = "address not found";
                }
            }
            else {
                document.getElementById("userlocation").innerHTML = "Geocoder failed due to: " + status;
            }
        }
    );
}

下面是HTML:

代码语言:javascript
复制
<body onload="getLocation()">
  <p id="userlocation">Getting your location ...</p>
</body>

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-10-02 09:27:57

在您的移动设备中,您必须在设置中为safari启用地理位置。如果地理位置被禁用,什么都不会附加。

尝试这种操作:

  • 进入设置。
  • 接下来,转到机密性(在滚动菜单的左侧)。
  • 然后,单击位置服务栏。
  • 向下滚动并接受该位置的狩猎服务。

如果你有什么问题告诉我

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46522161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档