我的网站上的地理位置存在地理位置问题。
实际上我只需要获得纬度/经度坐标。 在PC上,Android设备一切都很酷,在iPhone上它也很好,但只有我使用wifi连接。但是当我使用我的旧iPhone 5s使用3g或LTE时,我什么也没得到(但是从iphone 6开始它可以工作)。
我已经知道如果关闭wifi,Safari不支持地理定位。 但我仍然需要让它适用于iPhone 4,5等iDevices。 我正在使用这段示例代码:
{{1}}
答案 0 :(得分:2)
我是iPhone 5S的所有者,尝试通过3G进行地理定位,它就像一个魅力。我已经通过Wifi和3G尝试了CodePen而没有任何问题。
$(document).ready(function(){
setInterval(function(){
getLocation(function(position) {
//do something cool with position
currentLat = position.coords.latitude;
currentLng = position.coords.longitude;
$("#status").html(currentLat + " " + currentLng);
});
}, 1000);
});
var GPSTimeout = 10; //init global var NOTE: I noticed that 10 gives me the quickest result but play around with this number to your own liking
//function to be called where you want the location with the callback(position)
function getLocation(callback) {
if (navigator.geolocation) {
var clickedTime = (new Date()).getTime(); //get the current time
GPSTimeout = 10; //reset the timeout just in case you call it more then once
ensurePosition(callback, clickedTime); //call recursive function to get position
}
return true;
}
//recursive position function
function ensurePosition(callback, timestamp) {
if (GPSTimeout < 6000) {
//call the geolocation function
navigator.geolocation.getCurrentPosition(
function(position) //on success
{
//if the timestamp that is returned minus the time that was set when called is greater then 0 the position is up to date
if (position.timestamp - timestamp >= 0) {
GPSTimeout = 10; //reset timeout just in case
callback(position); //call the callback function you created
} else //the gps that was returned is not current and needs to be refreshed
{
GPSTimeout += GPSTimeout; //increase the timeout by itself n*2
ensurePosition(callback, timestamp); //call itself to refresh
}
},
function() //error: gps failed so we will try again
{
GPSTimeout += GPSTimeout; //increase the timeout by itself n*2
ensurePosition(callback, timestamp); //call itself to try again
}, {
maximumAge: 0,
timeout: GPSTimeout
}
)
}
}
感谢Chris Beckett的样本。
如果您无法提供更多详细信息,或许可以提供更多详细信息吗?
Ps:只需编辑以确保每个需要地理位置的人检查是否启用了Safari的位置服务: