javascript - Google map API 不适用于 IOS safari

标签 javascript html ios google-maps-api-3 safari

更新:它只能在 Safari 移动设备上运行(iOS 版 Chrome 可以正常运行)。

我正在尝试使用谷歌地图 API 获取用户位置。

它在桌面 FF 和 Chrome 上正常工作(显示城市和国家/地区),但在 iOS safari (iOS 11) 上不显示任何内容。此外,它似乎在其他一些移动设备上无法正常工作。

请注意,我使用的是 HTTPS,因此不存在安全问题。

这是 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:

<body onload="getLocation()">
  <p id="userlocation">Getting your location ...</p>
</body>

提前致谢。

最佳答案

在您的移动设备中,您必须在设置中启用 Safari 的地理定位。如果禁用地理位置,则不会附加任何内容。

尝试这个操作:

  • 前往设置。
  • 接下来,转到 secret 性(将手放在滚动菜单的左侧)。
  • 然后,点击位置服务栏。
  • 向下滚动并接受该位置的狩猎服务。

如果您有疑问请告诉我

关于javascript - Google map API 不适用于 IOS safari,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46522161/

相关文章:

ios - 动画 UIImageView 逐行出现在屏幕上

javascript - 篝火算法挑战赛: Where Do I Belong on javascript

javascript - 上传图片到 Canvas

objective-c - 像这样的 UITabBar

html - 为什么我的滚动条没有出现?

html - 使用CSS在多个div中水平居中图像

ios - 无效更新 : invalid number of items on UICollectionView

javascript - WebAudio setTargetAtTime : formula to convert-time constant to seconds

javascript - html 中的自毁按钮

javascript - 如何使用javascript在录音后将麦克风静音