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>