<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script>
function getDistance(lat1, lon1, lat2, lon2) {
var radLat1 = (lat1 * Math.PI) / 180; //将角度换算为弧度
var radLat2 = (lat2 * Math.PI) / 180; //将角度换算为弧度
var a = radLat1 - radLat2;
var b = (lon1 * Math.PI) / 180 - (lon2 * Math.PI) / 180;
var 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 * 6378137.0; // 取WGS84标准参考椭球中的地球长半径(单位:m)
//s = Math.round(s * 10000) / 10000; //两点之间距离(保留四位)
return s; //(单位:m)
}
</script>
</head>
<body>
<script>
alert(getDistance(39.923423,116.368904,39.922501,116.387271));
</script>
</body>
</html>
//判断是否位于打卡圈内(单位:m)
public bool isInCircle(double lat1, double lon1, double lat2, double lon2, double radius)
{
bool isIn = false;
double radLat1 = (lat1 * Math.PI) / 180; //将角度换算为弧度
double radLat2 = (lat2 * Math.PI) / 180; //将角度换算为弧度
double a = radLat1 - radLat2;
double b = (lon1 * Math.PI) / 180 - (lon2 * Math.PI) / 180;
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 * 6378137.0; // 取WGS84标准参考椭球中的地球长半径(单位:m)
s = Math.Round(s * 10000) / 10000; //两点之间距离(保留4位小数)
if (s < radius)
{
//两点之间的距离小于给定半径,处于打卡范围内
isIn = true;
}
return isIn;
}
//CalDistance("116.45925,39.910031", "116.587922,40.081577");
/// <summary>
/// 计算两点行车距离
/// </summary>
/// <param name="origins">起点坐标(例:116.45925,39.910031)</param>
/// <param name="destination">终点坐标(例:116.587922,40.081577)</param>
/// <returns></returns>
public string CalDistance(string origins, string destination)
{
string res = "";
string key = "7abfed2964bcad2143b01f6aaf0b86dssa";
if (origins == "" || destination == "")
{
return JsonConvert.SerializeObject(new { code = "0", msg = "起点或终点坐标为空" });
}
string url = "https://restapi.amap.com/v3/direction/driving?origin=116.45925,39.910031&destination=116.587922,40.081577&output=JSON&key=" + key;//驾车路径规划
url = "https://restapi.amap.com/v3/distance?origins=" + origins + "&destination=" + destination + "&type=1&output=JSON&key=" + key;//直接计算驾车距离
res = HttpGet(url);
if (!string.IsNullOrEmpty(res))
{
JObject jobj = (JObject)JsonConvert.DeserializeObject(res);
if (jobj["status"].ToString() == "1")//成功
{
res = JsonConvert.SerializeObject(new { code = "1", info = jobj["results"][0]["distance"].ToString() });
}
else
{
res = JsonConvert.SerializeObject(new { code = "0", msg = jobj["info"].ToString() });
}
}
return res;
}
/// <summary>
/// Get请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="Timeout">超时时间默认两分钟</param>
/// <returns></returns>
public static string HttpGet(string url, int Timeout = 120000)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = Timeout;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}