2017年9月13日 星期三

取的經緯度位置,使用navigator物件的geolocation屬性的getCurrentPosition方法,產生的事件物件的coords屬性產生latitude與longitude屬性


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="content"></div>
<button id="btn">取得位置</button>
<script type="text/javascript">
document.getElementById('btn').onclick=function(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
document.getElementById('content').innerHTML=
'你的位置緯度是:'+position.coords.latitude+'<br>'+
'你的位置經度是:'+position.coords.longitude;
});
}else{
document.getElementById('content').innerHTML='不支援喔'
}
}
</script>
</body>
</html>


沒有留言:

張貼留言