【问题标题】:Google Maps API doesn't work on IOS safariGoogle Maps API 不适用于 IOS Safari
【发布时间】:2018-03-13 07:50:29
【问题描述】:

更新:它不仅适用于 safari mobile(chrome for ios 可以正常工作)。

我正在尝试使用 google maps 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 自己的浏览器窗口中加载 Google Maps API javascript 脚本链接。一旦我这样做了,由于代理未启用而显示证书错误

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


【解决方案1】:

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

试试这个操作:

  • 进入设置。
  • 接下来,转到机密信息(手放在滚动菜单的左侧)。
  • 然后,点击定位服务栏。
  • 向下滚动并接受该位置的 Safari 服务。

如果您有任何问题,请告诉我

【讨论】:

  • 定位服务开启,一切都应该正确。我在我的网站上接受了位置使用。不知道出了什么问题。
  • 除了定位服务,您还可以在 iOS 的 Safari 设置下找到定位设置。在我的情况下,即使 Safari 设置设置为允许,位置服务也会覆盖它,因此您可能需要仔细检查这两个设置是否都设置为询问/允许。
猜你喜欢
  • 1970-01-01
  • 2011-09-04
  • 2013-09-20
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 2014-03-28
  • 2015-05-07
相关资源
最近更新 更多