用js获取当前地理位置的经纬度

<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>H5定位</title>
<style>
*{
padding: 0;
margin: 0;
list-style: none;
}
h1{
text-align: center;
margin: 20px auto;
color: darkgoldenrod;
font-weight: bold;
}
button{
display: block;
width: 50%;
height: 40px;
line-height: 40px;
font-weight: bold;
text-align: center;
border: none;
margin: 30px auto;
border-radius: 3px;
color: #fff;
font-size: 14px;
box-shadow: 4px 4px 5px rgba(0,0,0,0.5);
}
.bt1{
background: green;
}
.bt2{
background:#DB7093;
}
span{
display: block;
text-align: center;
color: deeppink;
}
a{
display: block;
line-height: 24px;
text-align: center;
margin: 10px auto;
}
.footer{
position: absolute;
left: 0;
bottom: 20px;
width: 100%;
color: #474747;
font-size: 12px;
text-align: center;
}
</style>
</head>
<body>
<h1>H5定位</h1>
<p id="pId"></p>
<button class="bt1" οnclick="getLocation()"> 我的经纬度 </button>
<span>点下试试吧!可以获得你的经纬度。</span>
</body>
<script>
var x=document.querySelector("#pId")
function getLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
}else{
x.innerHTML="定位失败";
}
}
function showPosition(position){
x.innerHTML="我当前经度为"+position.coords.latitude+",纬度为"+position.coords.longitude+"的地方哦"
}
</script>
</html>

转载于:https://www.cnblogs.com/zhuyupingit/p/6322211.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值