ヘッダに
<script>
// geolocationを利用できるか確認
if (navigator.geolocation) {
//現在位置を取得する
navigator.geolocation
.getCurrentPosition(success,error,options);
var options = {
// enableHighAccuracyは、GPSの精度でtrueかfalseをセットする
// trueだと精度が高くなる
enableHighAccuracy: true,
//timeoutは、タイムアウト時間でミリ秒単位
timeout: 1000,
// maximumAgeは、キャッシュ有効時間でミリ秒で表す
// 0の場合、常に最新の情報を取得する
maximumAge: 0
};
// 成功時
function success(position) {
var crd = position.coords;
var target = document.getElementById("btn-a");
// Positionオブジェクトの中にあるcoordsオブジェクトの
// latitudeとlongitudeプロパティを参照
document.getElementById("ido")
.textContent = "経度 : " + crd.latitude;
document.getElementById("keido")
.textContent = "緯度 : " + crd.longitude;
document.getElementById("idokeido")
.textContent = + crd.latitude +"," + crd.longitude;
target.href = "https://www.google.co.jp/maps/place/" + crd.latitude +"," + crd.longitude; ;
};
// 失敗時
function error(err) {
// PositionErrorオブジェクトの
// codeとmessageプロパティを参照
document.getElementById("ido").textContent
= "エラーコード : " + err.code;
document.getElementById("keido").textContent
= "エラーメッセージ : " + err.message;
};
} else {
document.getElementById("test1").textContent
= "このブラウザではGeolocationの使用はできません";
}
</script>
そして
書きたい場所に
<p id="ido"></p> <p id="keido"></p>
と書けば、
↓↓↓↓↓↓↓↓
グーグルマップで見るときは
↓↓↓↓↓↓↓↓
↑↑↑↑↑↑↑↑をコピ-して検索欄に貼り付ける
ボタンを作るときは、
<a id="btn-a" href="" target="_blank" class="button-a" rel="noopener">グーグルマップで開く</a>

コメント