Html5获取经纬度 Asp.net 根据经纬度计算距离(可用于上下班打卡)

手机端根据位置签到打卡

通过JS获取经纬度

<div id="show">
纬度:<span id="latitude"></span><br />
经度:<span id="longitude"></span><br />
准确度:<span id="accuracy"></span>
</div>
<script type="text/javascript">
    var doc = document,
        latitude = doc.getElementById('latitude'),
        longitude = doc.getElementById('longitude'),
        accuracy = doc.getElementById('accuracy'),
        support = doc.getElementById('support'),
        showDiv = doc.getElementById('show');
    function lodeSupport() {
        if (navigator.geolocation) {
            showDiv.style.display = 'block';
            navigator.geolocation.getCurrentPosition(updataPosition);
        } else {
            support.innerHTML = '对不起,浏览器不支持!';
            showDiv.style.display = 'none';
        }
    }
    function updataPosition(position) {
        var latitudeP = position.coords.latitude,
            longitudeP = position.coords.longitude,
            accuracyP = position.coords.accuracy;
        latitude.innerHTML = latitudeP;
        longitude.innerHTML = longitudeP;
        accuracy.innerHTML = accuracyP;
        //在百度 map中显示地址
        //var map = new BMap.Map("map_canvas");          // 创建地图实例  
        //var point = new BMap.Point(longitudeP, latitudeP);  // 创建点坐标  
        //map.centerAndZoom(point, 15);// 初始化地图,设置中心点坐标和地图级别  
        //var marker = new BMap.Marker(point);
        //map.addOverlay(marker);


    }
    window.addEventListener('load', lodeSupport, true);
</script>

计算打卡地点和公司地点的距离

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                double lat1 = 120.42446033212362; //公司经度
                double lng1 = 36.17011720520026;  //公司纬度


               double lat2 = 120.42446033212252; //打卡经度 
               double lng2 = 36.17011720519916; //打卡纬度
               double s = GetDistance(lat1, lng1, lat2, lng2);
               Response.Write(s);
            }
        }
        private const double EARTH_RADIUS = 6378.137; //地球半径
        private static double rad(double d)
        {
            return d * Math.PI / 180.0;
        }
        public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
        {
            double radLat1 = rad(lat1);
            double radLat2 = rad(lat2);
            double a = radLat1 - radLat2;
            double b = rad(lng1) - rad(lng2);
            double s = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) +
             Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
            s = s * EARTH_RADIUS;
            s = Math.Round(s * 10000) / 10000;
            return s;
        }

 

你可能感兴趣的:(Html5获取经纬度 Asp.net 根据经纬度计算距离(可用于上下班打卡))