1. 申请key值
参考此篇博客:https://blog.csdn.net/chenwen112/article/details/102910437
2. 引入地图
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.3&key=您申请的key值"></script>
3. 功能实现
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta http-equiv="Access-Control-Allow-Origin" content="*" />
<link rel="stylesheet" href="../../css/mui.min.css">
<link rel="stylesheet" type="text/css" href="../../css/app.css" />
<link rel="stylesheet" type="text/css" href="../../css/icons-extra.css" />
<link rel="stylesheet" type="text/css" href="../../css/mui.picker.min.css">
<title>定位打卡</title>
<style type="text/css">
#iCenter {
width: 95%;
height: 150px;
border: 1px #000 solid;
margin: 20px auto;
}
.a {
color: #8F8F94;
width: 55px;
height: 55px;
text-align: center;
display: block;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 5px;
float: left;
margin: 10px 10px 5px 15px;
}
</style>
</head>
<body onload="startTime()">
<div id="iCenter"></div>
<div class="mui-card">
<form class="mui-input-group">
<div class="mui-input-row">
<label><span class="mui-icon mui-icon-paperplane" style="font-size: medium;"></span>签到时间:</label>
<label id="attentime"></label>
</div>
<div class="mui-input-row">
<label> <span class="mui-icon mui-icon-location-filled" style="font-size: medium;"></span>签到地点:</label>
<label id="addr"></label>
</div>
</form>
</div>
<div class="mui-card" style="height: 20%;">
<div class=" mui-card-media">
<img class="a" id="img" />
<label for="myFile">
<span id="format" class="a mui-icon mui-icon-camera" style="font-size: -webkit-xxx-large;"></span>
</label>
<input style="display: none;" type="file" capture="camera" id="myFile" onchange="upload('#myFile', '#img');" />
</div>
</div>
<div style="background: #e0e0e0;">
<button id="succbtn" type="button" class="mui-btn mui-btn-link mui-text-center" style="width: 100%;font-size: large;background: #e0e0e0; position: fixed;bottom:0px;">提交</button>
</div>
<script src="../../js/mui.js"></script>
<script src="../../js/mui.picker.min.js"></script>
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.3&key=申请的key值"></script>
<script src="../../js/host.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
/**
* 定位
*/
var mapObj = new AMap.Map('iCenter');
mapObj.plugin('AMap.Geolocation', function() {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true, // 是否使用高精度定位,默认:true
timeout: 10000, // 超过10秒后停止定位,默认:无穷大
maximumAge: 0, // 定位结果缓存0毫秒,默认:0
convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true
showButton: true, // 显示定位按钮,默认:true
buttonPosition: 'LB', // 定位按钮停靠位置,默认:'LB',左下角
buttonOffset: new AMap.Pixel(10, 20), // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true
showCircle: true, // 定位成功后用圆圈表示定位精度范围,默认:true
panToLocation: true, // 定位成功后将定位到的位置作为地图中心点,默认:true
zoomToAccuracy: true // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
});
mapObj.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', onComplete); // 返回定位信息
AMap.event.addListener(geolocation, 'error', onError); // 返回定位出错信息
});
function onComplete(obj) {
// var res = '经纬度:' + obj.position +
// '\n精度范围:' + obj.accuracy +
// '米\n定位结果的来源:' + obj.location_type +
// '\n状态信息:' + obj.info +
// '\n地址:' + obj.formattedAddress +
// '\n地址信息:' + JSON.stringify(obj.addressComponent, null, 4);
var addr = obj.formattedAddress;
document.getElementById("addr").innerText = addr;
localStorage.setItem('addr', addr);
};
function onError(obj) {
// alert(obj.info + '--' + obj.message);
console.log(obj);
};
</script>
</html>