我一直在努力:http://davidcool.com/feeds
除了Safari之外,它在所有浏览器中都能正常工作。有时Safari会在无限循环中请求地理定位权限,有时它会像它应该的那样只询问一次,大多数情况下它会要求3次...所有其他浏览器按计划询问一次。
以下是代码:
function success(position) {
//output lat+long data
//console.log(position);
//var s = document.querySelector('#status');
//s.innerHTML = "Latitude: " + position.coords.latitude + "<br />Longitude: " + position.coords.longitude + "<br />";
var s = document.querySelector('#status');
if (s.className == 'success') {
// not sure why we're hitting this twice in FF, I think it's to do with a cached result coming back
return;
}
s.innerHTML = "success";
s.className = 'success';
//get timezone info
$.ajax({
url:'https://maps.googleapis.com/maps/api/timezone/json?location=' + position.coords.latitude + ',' + position.coords.longitude + '×tamp=' + Math.floor(position.timestamp/1000) + '&language=en&key=????',
dataType: "json",
async:false,
cache:false,
error:
function(res){
console.log(res);
},
success:
function(res){
//console.log(res);
time_zone_id = res.timeZoneId;
time_zone_name = res.timeZoneName;
//var t = document.querySelector('#status1');
//t.innerHTML = "Time Zone ID: " + res.timeZoneId + "<br />Time Zone Name: " + res.timeZoneName + "<br />" ;
}
});
//get address info
$.ajax({
url:'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&language=en&key=????',
dataType: "json",
async:false,
cache:false,
error:
function(res){
console.log(res);
},
success:
function(res){
//console.log(res);
city = res.results[2].address_components[1].long_name;
address = res.results[2].formatted_address;
//var t = document.querySelector('#status2');
//t.innerHTML = "Address: " + res.results[0].formatted_address ;
}
});
//get weather info
$('#weather').load("weather.php", {'latitude':position.coords.latitude, 'longitude':position.coords.longitude, 'local_time_zone_id':time_zone_id, 'local_time_zone_name':time_zone_name, 'city':city, 'address':address});
};
function error(msg) {
var s = document.querySelector('#weather');
//s.innerHTML = typeof msg == 'string' ? msg : "Failed to get location. Try refreshing.";
s.innerHTML = typeof msg == 'string' ? msg : "Failed to get location. Try refreshing.";
s.className = 'fail';
};
//get lat+long info
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
//setTimeout(navigator.geolocation.getCurrentPosition(success, error), 500);
} else {
error('not supported');
};
我看过其他人有同样的问题,但未能找到明确答案的原因。有什么想法吗?
有趣的是,在我的开发Mac上,它只会一直询问,在另一台开发Mac上它会多次询问!相同的操作系统和相同版本的Safari 8.0.6。
答案 0 :(得分:0)
我真的不是100%肯定,但我遇到了同样的问题。然后,我删除了msg
回调中的error
参数,现在它似乎在Safari中正常工作。
所以你的error
回调应该是
function error() {
var s = document.querySelector('#weather');
s.innerHTML = "Failed to get location";
s.className = 'fail';
}
让我知道这是否有效!