1.使用腾讯位置服务可以完美的解决问题,已使用测试;
2.具体的解决方案:
调用方式1:引用封装好的JS模块,调起前端的定位组件,通过封装好的接口获取位置信息。
js引入地址:https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>前端定位模块</title>
<meta name="viewport"content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
<style>
* {
margin: 0;
padding: 0;
border: 0;
}
body {
position: absolute;
width: 100%;
height: 100%;
text-align: center;
}
#pos-area {
background-color:#009DDC;
margin-bottom: 10px;
width: 100%;
overflow: scroll;
text-align: left;
color: white;
}
#demo {
padding: 8px;
font-size: small;
}
#btn-area {
height: 100px;
}
button {
margin-bottom: 10px;
padding: 12px 8px;
width: 42%;
border-radius: 8px;
background-color:#009DDC;
color: white;
}
</style>
<script type="text/javascript"src="
https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js
"></script>
</head>
<body>
<div id="pos-area">
<p id="demo">点击下面的按钮,获得对应信息:<br /></p>
</div>
<div id="btn-area">
<button onclick="geolocation.getLocation(showPosition, showErr, options)">获取精确定位信息</button>
<button onclick="geolocation.getIpLocation(showPosition, showErr)">获取粗糙定位信息</button>
<button onclick="showWatchPosition()">开始监听位置</button>
<button onclick="showClearWatch()">停止监听位置</button>
</div>
<script type="text/JavaScript">
vargeolocation = newqq.maps.Geolocation("OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77","myapp");
document.getElementById("pos-area").style.height = (document.body.clientHeight - 110) + 'px';
varpositionNum = 0;
varoptions = {timeout: 8000};
functionshowPosition(position) {
positionNum ++;
document.getElementById("demo").innerHTML += "序号:"+ positionNum;
document.getElementById("demo").appendChild(document.createElement('pre')).innerHTML = JSON.stringify(position, null, 4);
document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight;
};
functionshowErr() {
positionNum ++;
document.getElementById("demo").innerHTML += "序号:"+ positionNum;
document.getElementById("demo").appendChild(document.createElement('p')).innerHTML = "定位失败!";
document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight;
};
functionshowWatchPosition() {
document.getElementById("demo").innerHTML += "开始监听位置!<br /><br />";
geolocation.watchPosition(showPosition);
document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight;
};
functionshowClearWatch() {
geolocation.clearWatch();
document.getElementById("demo").innerHTML += "停止监听位置!<br /><br />";
document.getElementById("pos-area").scrollTop = document.getElementById("pos-area").scrollHeight;
};
</script>
</body>
</html>
3.原创作品,请勿转载
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。