更新:它不仅在safari上工作(chrome正确工作)。
我正在尝试使用google获取用户位置。
它在桌面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>提前谢谢。
发布于 2017-10-02 09:27:57
在您的移动设备中,您必须在设置中为safari启用地理位置。如果地理位置被禁用,什么都不会附加。
尝试这种操作:
如果你有什么问题告诉我
https://stackoverflow.com/questions/46522161
复制相似问题