【发布时间】: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