<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
<style type="text/css">
html, body, #container {
height: 100%;
}
.info {
width: 20rem;
}
</style>
</head>
<body>
<div id="container"></div>
<div class="info">
<h4 id='status'></h4>
<hr>
<p id='result'></p>
<hr>
<p>由于众多浏览器已不再支持非安全域的定位请求,为保位成功率和精度,请升级您的站点到HTTPS。</p>
</div>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.7&key=aef0adeda4d33acf723d2113ec657ece"></script>
<script type="text/javascript">
var map = new AMap.Map('container', {
resizeEnable: true
});
AMap.plugin('AMap.Geolocation', function () {
var geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
buttonPosition: 'RB',
buttonOffset: new AMap.Pixel(10, 20),
zoomToAccuracy: true,
});
map.addControl(geolocation);
geolocation.getCurrentPosition(function (status, result) {
if (status == 'complete') {
onComplete(result)
} else {
onError(result)
}
});
});
function onComplete(data) {
document.getElementById('status').innerHTML = '定位成功'
var str = [];
str.push('定位结果:' + data.position);
str.push('定位类别:' + data.location_type);
if (data.accuracy) {
str.push('精度:' + data.accuracy + ' 米');
}
str.push('是否经过偏移:' + (data.isConverted ? '是' : '否'));
document.getElementById('result').innerHTML = str.join('<br>');
}
function onError(data) {
document.getElementById('status').innerHTML = '定位失败'
document.getElementById('result').innerHTML = '失败原因排查信息:' + data.message;
}
</script>
</body>
</html>
