关键点:获取当前位置、设置考勤点、计算两点距离判断是否在范围内。
<script type="text/javascript" src="http://api.map.baidu.com/api?此处为秘钥"></script>
<div id="allmap"></div>
获取当前位置:
var map
var pointNow;
var pointAttendance;
function GetLocation() {
// 百度地图API功能
map = new BMap.Map("allmap");
map.enableScrollWheelZoom(true);
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function (r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
pointNow = r.point;
map.centerAndZoom(pointNow, 15);
var mk = new BMap.Marker(pointNow);
map.addOverlay(mk);
map.panTo(pointNow);
//alert('您的位置:' + r.point.lng + ',' + r.point.lat);
}
else {
alert('failed' + this.getStatus());
}
}, { enableHighAccuracy: true })
}
设置考勤点:
function SetAttendance(longitude, latitude) {
pointAttendance = new BMap.Point(longitude, latitude);
circle = new BMap.Circle(pointAttendance, 300, {
fillColor: "blue",
strokeWeight: 1,
fillOpacity: 0.2,
strokeOpacity: 0.2
});// 显示签到点的位置(半径为300米的一个圆)
map.addOverlay(circle);
//计算当前位置与考勤点距离
var distance = map.getDistance(pointNow, pointAttendance).toFixed(2);
if (distance <= 300) {
//在范围内
} else {
//不在范围内
}
}