Google Maps API不适用于IOS Safari

时间:2017-10-02 08:40:14

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

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

我正在尝试使用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>

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

尝试这种操作:

  • 转到设置。
  • 接下来,转到机密(用滚动菜单左侧的一只手)。
  • 然后,点击位置服务栏。
  • 向下滚动并接受该地点的野生动物园服务。

如果您有疑问,请告诉我