gpt4 book ai didi

javascript - Google map API 不适用于 IOS safari

转载 作者:行者123 更新时间:2023-11-28 04:08:10 31 4
gpt4 key购买 nike

更新:它只能在 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com