2012-03-31 4 views
0
<script> 
navigator.geolocation.getCurrentPosition(show_position); 
//This function does the work of getting the actual user coordinates 
function show_position(position){ 

var lat = position.coords.latitude; 
var longi = position.coords.longitude; 
var tick = position.timestamp; 

document.getElementById("Coord1").innerHTML=lat; 
document.getElementById("Coord2").innerHTML=longi; 

} 
</script> 

<p>Your Latitude is: <b id="Coord1" style="color:blue;"> </b></p> 
<p>Your Longitude is: <b id="Coord2" style="color:blue;"> </b> </p> 

안녕 모두,. 그러나 navigator.geolocation.getCurrentPosition(show_position);을 실행하는 javascript 버튼을 만들 수 있습니까? 검색 상자가 있고 그 옆에 버튼이 있어야하고 사용자가 클릭하면 자동으로 빈 상자를 채운 다음 검색합니다. 가능한지 아닌지는 알 수 없습니다. 당신은 클릭 이벤트자바 스크립트 HTML5 위치 버튼

x$("#btnInit").click(loadDemo); 
    //run the function 
    function loadDemo() { 
navigator.geolocation.getCurrentPosition(show_position); 
//This function does the work of getting the actual user coordinates 
function show_position(position){ 

var lat = position.coords.latitude; 
var longi = position.coords.longitude; 
var tick = position.timestamp; 

document.getElementById("Coord1").innerHTML=lat; 
document.getElementById("Coord2").innerHTML=longi; 

} 
} 

에 navigator.geolocation를 실행할 수

답변

0

사전에

덕분에, 나는 버튼의 전면에 따라서는 X $를 xuijs를 사용하여. document.getElementById ("btnInit")와 동일합니다.

페이지의 id가 btnInit 인 버튼이 있습니다.

0
<script>  
document.getElementById('getPositionBtn').addEventListener('click', function() { 
    navigator.geolocation.getCurrentPosition(function(position) {   
     document.getElementById("Coord1").innerHTML=position.coords.latitude; 
     document.getElementById("Coord2").innerHTML=position.coords.longitude; 
    }); 
}); 
</script> 

<p>Your Latitude is: <b id="Coord1" style="color:blue;"> </b></p> 
<p>Your Longitude is: <b id="Coord2" style="color:blue;"> </b> </p><br> 
<button id="getPositionBtn">Get Position!</button>