2017-08-24 4 views
2

初めてnavigator.geolocation.getCurrentPosition(success, error, options);を実行すると、ユーザーの所在地を取得できます。私はこの仕事を得ることができますどのように、私は成功しませんthis question's answersで与えられたアドバイスに従っているnavigator.geolocationが2回目の実行でエラーを返す - IE

The current position could not be determined.

:しかし、上の第二の実行から、関数はエラーを返しますか?

ここにはworking fiddleがあり、エラーをすばやく確認できます。

//Pass this options to the getCurrentPosition 
var options = { 
    enableHighAccuracy: true, 
    timeout: 5000, 
    maximumAge: 0 
}; 
//function to execute if the current position was succesfully retrieved 
function success(pos) { 
console.log(pos); 
    var crd = {lat: pos.coords.latitude, lng : pos.coords.longitude }; 
    var myPre = document.querySelector('pre'); 
    myPre.textContent = JSON.stringify(crd); 
    myPre.style.color = someColor(); // use a diferent color just to see it's a new execution of the code 
}; 
//execute this on error 
function error(err) { 
    var myPre = document.querySelector('pre'); 
    myPre.textContent = err; 
    myPre.style.color = someColor(); // use a diferent color 
}; 
//attach function to button 
var myButton = document.querySelector('button'); 
myButton.addEventListener('click', function(){ 
    navigator.geolocation.getCurrentPosition(success, error, options); 
}); 
+0

は、私は意図的にコードスニペットを追加していなかったので、それが反映されていない版をロールバックこのエラーは有用ではありません。したがって、エラーが見られ、テストされることができるフィドル。 – randomguy04

答えて

1

私の考えは以下の通りです:

IEのユーザーのみ(デフォルト設定によって)ウェブサイト(スクリプト)が一度getCurrentLocationを実行することができます。ユーザーは、複数回実行するための例外を許可する必要があります。

However I don't know (and can't really find any documentation) if this behaviour is by design or a bug. The solution below is a work-around.

代わりに、最初の大成功の後に使用watchpositionこのバグを一周します。更新されたフィドルを参照してください:https://jsfiddle.net/b2rnr7tw/6/

このフィドルで私はwatchPositionを設定し、それが更新されるとすぐに新しい場所を示します。その後、キャンセルされます(それ以外の場合は更新されます)。

//Pass this options to the getCurrentPosition 
var options = { 
    enableHighAccuracy: true, 
    timeout: 5000, 
    maximumAge: 0 
}; 

var watch = null; 
var watchId = null; 
//function to execute if the current position was succesfully retrieved 
function success(pos) { 

    var crd = {lat: pos.coords.latitude, lng : pos.coords.longitude }; 
    var myPre = document.querySelector('pre'); 
    myPre.textContent = JSON.stringify(crd); 
    myPre.style.color = someColor(); // use a diferent color 
    watch.clearWatch(watchId); //after success clear the watchId. 
}; 
//execute this on error 
function error(err) { 
    var myPre = document.querySelector('pre'); 
    myPre.textContent = err; 
    myPre.style.color = someColor(); // use a diferent color 
    //keep running the watchPosition if on error, however you can use a counter to only try it a few times (recommended) 
}; 
//attach function to button 
var myButton = document.querySelector('button'); 
myButton.addEventListener('click', function(){ 
    if (!watch) 
    { 
    watch = navigator.geolocation; 
    watch.getCurrentPosition(success, error, options); 
    } 
    else 
    { 
    watchId = watch.watchPosition(success, error, options); 
    } 
}); 
+1

ありがとうございます、どちらのドキュメントも見つかりませんでした。あなたのメソッドは動作しますが、2回目の実行から、getCurrentPositionの代わりにwatchPositionの実行を開始すると、エラー関数と成功関数が実行されるので、それはエラーを投げた直後に、テキストがLatとLngの値で更新された直後に、いくつかの異なるコンピュータで試してみましたが、これも同様に得られますか?私は関数のドキュメントを見ましたが、送信されるパラメータの順序はきれいです – randomguy04

+0

私は視覚的な振る舞いも持っていますが、それは簡単なコードで簡単に修正できます。 – Mouser

1

私はIE11でMouserのソリューションを使用していましたが、Edgeを壊すので、ブラウザの検出が必要です。ここでIE11、エッジ14でテスト私の解決策があり、FFXとChrome(執筆時点でFFXやChromeの最新バージョン)

  var currentPositionHasBeenDisplayed = false; 

      if (navigator.geolocation) { 

       var options = {}; 

       var isIE = document.documentMode; //IE 8+ 

       // IE only allows one call per script to navigator.geolocation.getCurrentPosition, so we need a workaround 
       if (currentPositionHasBeenDisplayed == true && isIE) { 
        navigator.geolocation.watchPosition(
         function (pos) { 
          var myLatLng = new google.maps.LatLng(parseFloat(pos.coords.latitude), parseFloat(pos.coords.longitude)); 
          map.setCenter(myLatLng); 
         }, 
         function (error) { }, 
         options); 
       } 
       else { 
        navigator.geolocation.getCurrentPosition(
         function (pos) { 
          var myLatLng = new google.maps.LatLng(parseFloat(pos.coords.latitude), parseFloat(pos.coords.longitude)); 
          map.setCenter(myLatLng); 
          currentPositionHasBeenDisplayed = true; 
         }, 
         function (error) { return false; }, 
         options); 
       } 
      } 
関連する問題