わかりました。ユーザーのジオロケーションを取得するものがあります。このユーザーは移動している可能性があるため、3秒ごとにジオロケーション関数を再実行するように設定されています。移動している場合は進行状況が表示されますが、移動していない場合、たとえば緯度と経度が同じである場合は、理由がないため、更新しないでください。
私の考えは次のとおりです。
function startGeolocation() {
var options;
navigator.geolocation.getCurrentPosition(geoSuccess, geoFail, options);
}
//get various coordinates
function geoSuccess(position) {
var gpsPosition = position;
var coordinates = gpsPosition.coords;
//if its the first run through and myLat is empty, then continue as normal.
if (!myLat){
myLat = coordinates.latitude;
myLong = coordinates.longitude;
//if its the second run through and the myLat is the same as it was before, do nothing.
}else if ((myLat == myLatSame) && (myLong == myLongSame)){
}
//else if they are different, e.g user has moved, then update.
else{
myLat = coordinates.latitude;
myLong = coordinates.longitude;
setTimeout(geoSuccess, 3000);
}
myLatSame = myLat;
myLongSame = myLong;
}
動作していないようで、ページはマップの読み込みを完全に停止します。
ただし、非常に基本的なコードに戻ると、
function startGeolocation() {
var options;
navigator.geolocation.getCurrentPosition(geoSuccess, geoFail, options);
setTimeout(startGeolocation, 3000);
}
function geoSuccess(position) {
var gpsPosition = position;
var coordinates = gpsPosition.coords;
myLat = coordinates.latitude;
myLong = coordinates.longitude;
これは正常に機能し、3秒ごとに更新されます。
私はjavascriptのコーディングに関する長い休止から戻ってきたので、私の構文と方法論は少し錆びています。前もって感謝します
編集:
コードにいくつかのアラートを追加しました。最初の実行時に次のことが発生します。最初の実行では、myLatおよびmyLongに対してalert(before if)=undefinedが発生します。!myLatは何も保持しないため、myLatとmyLongが座標で満たされ、アラート(in if)でアラートが送信されるため、次のアラート(myLatSame + myLongSame)が「NaN」として返されます。
else ifは同じではないためトリガーされませんが、else alert(else)ステートメントもトリガーされず、表示されません。
//get various coordinates
function geoSuccess(position) {
var gpsPosition = position;
var coordinates = gpsPosition.coords;
alert("before if \n" + myLat + "\n" + myLong);
//if its the first run through and myLat is empty, then continue as normal.
if (!myLat){
myLat = coordinates.latitude;
myLong = coordinates.longitude;
alert("in if \n" + myLat + "\n" + myLong);
alert(myLatSame + myLongSame);
//if its the second run through and the myLat is the same as it was before, do nothing.
}else if ((myLat == myLatSame) && (myLong == myLongSame)){
alert("in else if \n" + myLat + "\n" + myLong);
}
//else if they are different, e.g user has moved, then update.
{
myLat = coordinates.latitude;
myLong = coordinates.longitude;
alert("else \n" + myLat + "\n" + myLong);
setTimeout(geoSuccess, 3000);
}
myLatSame = myLat;
myLongSame = myLong;
}