0

わかりました。ユーザーのジオロケーションを取得するものがあります。このユーザーは移動している可能性があるため、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;
}
4

2 に答える 2

1

navigator.geolocation.watchPosition()overの使用を検討することをお勧めしますgetCurrentPosition()

ユーザーに変更があると、watchPosition が起動します。ただし、オプションを使用してチェックする前に待機するように関数に指示することもできます。

使用例:

tracker = navigator.geolocation;

watchId = tracker.watchPosition(savePosition,  displayError, {enableHighAccuracy: true, timeout: 10000, maximumAge: 20000 }); 

maxiumAge はミリ秒単位の読み取り間の時間です。

于 2013-02-28T15:24:07.310 に答える
0

そうですね、変数が

myLatSame = myLat;
myLongSame = myLong;

スコープは、グローバル スコープではなく、関数自体に限定される場合があります。値は復元されていますか? 値を警告できますか? また、コンソールにエラーが表示されているかどうかを確認し、すべてのブラケットを閉じていることを確認してください。添付した現在のコード サンプルから、最後に右中かっこがありません。

編集: また、タイムアウト関数を最初の IF に追加する必要があります。

     if (!myLat){
  myLat = coordinates.latitude;
  myLong = coordinates.longitude;
alert("in if \n" + myLat + "\n" + myLong);
alert(myLatSame + myLongSame);
setTimeout(geoSuccess, 3000);
//if its the second run through and the myLat is the same as it was before, do nothing.
    }else if 
于 2013-02-26T21:05:33.363 に答える