2017-09-13 4 views
-1

以下、私はHTML5ジオロケーションAPIを使用するコードを持っています。私が読んだすべての例では、常にボタンをクリックして場所を表示します。しかし、私はユーザーがページを読み込むとすぐにそれを表示したいと思います。私は以下の私のコードでそれを持っていたと思ったが、うまくいきません。 HTMLHTML5ジオロケーションAPIをページにロードする方法/ボタンをクリックしないで表示する方法?

var showLocation = document.getElementById("storeWeather"); 

function userLocation() { 
    if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(showPoints); 
    } else { 
    showLocation.innerHTML = "Your browser does not support this feature."; 
    } 
} 

function showPoints (position) { 
    showLocation.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br> Longitude: " + position.coords.longitude; 

} 

答えて

1

<div id = "storeLocation"></div> 

// divがこれに置き換えます

var showLocation = document.getElementById("storeLocation"); 
... 
//end or you js code 
document.body.onload = function(){ 
    userLocation(); 
}; 
関連する問題