モバイル デバイスでコードを使用するとバグが発生します (PC の処理時間が速いため、問題は発生しません)。私が抱えている問題は、関数が連続して起動しないことに関係していると思われます。つまり、すべてのパラメーターが設定されていなくても、関数は引き続き実行されます。
基本的な前提は、JQuery Geolocation が呼び出され、緯度/経度データが onClick 関数を介して渡されるパラメーターと組み合わせて使用されることです。これは、このデータに基づく結果を返す AJAX に渡されます。
ただし、モバイル デバイスでのテスト中に、リンクをクリックすると、GPS が間に合わないことがありますが、それでも先に進みます。その結果、何も返されず、ユーザーには空白の画面が表示されます。十分な時間を与えて十分にクリックすれば、うまくいきます。これは常に発生するとは限りませんが、すべてのデータの準備が整っているわけではないようです。
私はいくつかのバリエーションを試しましたが、私は中級の JS コーダーにすぎないため、問題がどこにあるのかわかりません。フィードバックが大好きです。
if(navigator.geolocation) {/* Function for returning list of locations for a unique cuisine */
function locationList(clicked_id) {
navigator.geolocation.getCurrentPosition(
function(position) {
$.ajax({
url: 'search.php',
type: 'GET',
data: 'id=' + clicked_id + '&action=geolocation&latitude=' + position.coords.latitude + '&longitude=' + position.coords.longitude,
beforeSend: function () {
$('#location-list').html('<div class="ajax-load"><img src="img/ajax-loader.gif" /></a>');
},
success: function (msg) {
$('#location-list').html(msg);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('Error submitting request.');
}
});
}, /* position */
function() {
$('#location-list').html('<div class="toolbar"><h1>GPS Error</h1><a class="back" href="#home">Home</a></div><div class="scroll"><ul class="rounded"><li><p>To take advantage of this app you need the GPS capability of your phone turned on. Please check whether this is turned on or else the app will not work</p></li></ul></div>');
} /* no position so failure */
);
} /* locationList */
}/* navigator.geolocation */