2017-01-12 13 views
0

私の現在のサイトは現在の場所に完全に依存しています。現在、私は現在の場所を取得するためにジオロケーションを使用しています。しかし、現在の状況では、ユーザーは共有場所について混乱しています。だから私はクライアントPCの現在の場所を取得するためのカスタムポップアップ(ブートストラップ)を作成したい。私の現在の場所を取得するためのカスタムポップアップ

私の現在のコードは以下の通りです:

function getLocation() { 

    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(showPosition, error, {enableHighAccuracy:true,timeout:60000,maximumAge:0}); 
    } 
    else { 
     console.log("Geolocation is not supported by this browser."); 
    } 
} 

function error(error) { 
    console.log(error); 
} 

function showPosition(position) { 

    var latitude = position.coords.latitude, 
    longitude = position.coords.longitude; 

    console.log(latitude); 
    console.log(longitude); 
} 

この問題で私を助けてください。

+1

をコードの問題は何ですか? – Teemu

+0

ブートストラップを使いたい場合は、そのモーダルについては、読んでください。 – Roljhon

+0

@Teemuコードはうまくいきますが、ユーザからカスタム位置ポップアップではなくジオロケーションを取得するカスタムポップアップを作成します。 –

答えて

2

ブラウザのジオロケーション許可のポップアップを回る方法があります。 Chromeで、それは次のようになります。

Chrome geolocation example

ユーザーは、あなたがその場所を(これはセキュリティ機能で)読むことができます前に、[許可]をクリックする必要があります。 しかし、絶対に必要とする場合は、許可が拒否されたかどうかを検出し、許可を与える方法とその理由を説明することができます。

検出するために、同様の質問へのthis answerthis exampleを参照してくださいエラー "許可が拒否されました":

function getLocation() { 
 
    if (navigator.geolocation) { 
 
    navigator.geolocation.getCurrentPosition(showPosition, positionError); 
 
    } else { 
 
    console.log("Geolocation is not supported by this browser."); 
 
    } 
 
} 
 

 
function showPosition(position) { 
 
    // Success, can use position. 
 
    console.log("Your position is: " + position); 
 
} 
 

 
function positionError(error) { 
 
    if (error.PERMISSION_DENIED) { 
 
    console.log("Error: permission denied"); 
 
    // Your custom modal here. 
 
    showError('Geolocation is not enabled. Please enable to use this feature.'); 
 
    } else { 
 
    // Handle other kinds of errors. 
 
    console.log("Other kind of error: " + error); 
 
    } 
 
} 
 

 
function showError(message) { 
 
    // TODO 
 
} 
 

 
getLocation();

関連する問題