0
私はGeolocationを使用して自分のコンピュータの場所を見つけようとしています。段落をクリックすると、それが実行されるイベントリスナーに接続したかったのです。ここでジオロケーションを使って場所を見つける方法は?
は私のHTMLです:
<p id="para1">
Click to find location
</p>
そしてここでは、私のスクリプトです:
//Global variables
var elMap = document.getElementById("para1");
var msg = 'Sorry, we were unable to get your location';
if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(success, fail);
elMap.textContent = 'Checking location...';
} else {
elMap.textContent = msg;
}
//Event listeners
document.getElementById("para1").addEventListener("click", geoAPI);
function geoAPI() {
function success(position) {
msg = '<h3>Longitude:<br>';
msg += position.coords.longitude + '</h3>';
msg += '<h3>Latitude:<br>';
msg += position.coords.latitude + '</h3>';
elMap.innerHTML = msg;
}
function fail(msg) {
elMap.textContent = msg;
console.log(msg.code);
}
}