-
高德地图获取坐标距离_高德地图计算两坐标之间距离
2021-01-17 19:27:19最近在做与地图相关的应用,使用了高德地图,研究了下高德地图计算两坐标距离的方法,官网上提供的开发包中有相关的方法,但是我的产品中比较特殊,无法直接使用提供的方法,所以就自己封装了相关计算方法,供大家...最近在做与地图相关的应用,使用了高德地图,研究了下高德地图计算两坐标距离的方法,官网上提供的开发包中有相关的方法,但是我的产品中比较特殊,无法直接使用提供的方法,所以就自己封装了相关计算方法,供大家参考。
Java实现
首先定义一个用于存储经纬度的类,这里起个名字叫:LngLat
package amap;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
* 存储经纬度坐标值的类,单位角度
*
*@author jianggujin
*
*/
public final class LngLat implements Cloneable
{
/**
* 纬度 (垂直方向)
*/
public final double latitude;
/**
* 经度 (水平方向)
*/
public final double longitude;
/**
* 格式化
*/
private static DecimalFormat format = new DecimalFormat("0.000000", new DecimalFormatSymbols(Locale.US));
/**
* 使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点。
*
*@param longitude
* 地点的经度,在-180 与180 之间的double 型数值。
*@param latitude
* 地点的纬度,在-90 与90 之间的double 型数值。
*/
public LngLat(double longitude, double latitude)
{
this(longitude, latitude, true);
}
/**
* 使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点
*
*@param longitude
* 地点的经度,在-180 与180 之间的double 型数值。
*
*@param latitude
* 地点的纬度,在-90 与90 之间的double 型数值。
*@param isCheck
* 是否需要检查经纬度的合理性,建议填写true
*/
public LngLat(double longitude, double latitude, boolean isCheck)
{
if (isCheck)
{
if ((-180.0D <= longitude) && (longitude < 180.0D))
this.longitude = parse(longitude);
else
{
throw new IllegalArgumentException("the longitude range [-180, 180].");
// this.longitude = parse(((longitude - 180.0D) % 360.0D + 360.0D) %
// 360.0D - 180.0D);
}
if ((latitude < -90.0D) || (latitude > 90.0D))
{
throw new IllegalArgumentException("the latitude range [-90, 90].");
}
this.latitude = latitude;
// this.latitude = parse(Math.max(-90.0D, Math.min(90.0D, latitude)));
}
else
{
this.latitude = latitude;
this.longitude = longitude;
}
}
/**
* 解析
*
*@param d
*@return
*/
private static double parse(double d)
{
return Double.parseDouble(format.format(d));
}
public LngLat clone()
{
return new LngLat(this.latitude, this.longitude);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(latitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(longitude);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
LngLat other = (LngLat) obj;
if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude))
return false;
if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude))
return false;
return true;
}
public String toString()
{
return "lat/lng: (" + this.latitude + "," + this.longitude + ")";
}
}
计算工具类如下:
package amap;
/**
* 高德地图工具
*
*@author jianggujin
*
*/
public class AMapUtils
{
/**
* 根据用户的起点和终点经纬度计算两点间距离,此距离为相对较短的距离,单位米。
*
*@param start
* 起点的坐标
*@param end
* 终点的坐标
*@return
*/
public static double calculateLineDistance(LngLat start, LngLat end)
{
if ((start == null) || (end == null))
{
throw new IllegalArgumentException("非法坐标值,不能为null");
}
double d1 = 0.01745329251994329D;
double d2 = start.longitude;
double d3 = start.latitude;
double d4 = end.longitude;
double d5 = end.latitude;
d2 *= d1;
d3 *= d1;
d4 *= d1;
d5 *= d1;
double d6 = Math.sin(d2);
double d7 = Math.sin(d3);
double d8 = Math.cos(d2);
double d9 = Math.cos(d3);
double d10 = Math.sin(d4);
double d11 = Math.sin(d5);
double d12 = Math.cos(d4);
double d13 = Math.cos(d5);
double[] arrayOfDouble1 = new double[3];
double[] arrayOfDouble2 = new double[3];
arrayOfDouble1[0] = (d9 * d8);
arrayOfDouble1[1] = (d9 * d6);
arrayOfDouble1[2] = d7;
arrayOfDouble2[0] = (d13 * d12);
arrayOfDouble2[1] = (d13 * d10);
arrayOfDouble2[2] = d11;
double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0])
+ (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1])
+ (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));
return (Math.asin(d14 / 2.0D) * 12742001.579854401D);
}
}
最后边写一段测试代码测试一下:
package test;
import org.junit.Test;
import amap.AMapUtils;
import amap.LngLat;
public class AMapTest
{
@Test
public void Test()
{
LngLat start = new LngLat(116.368904, 39.923423);
LngLat end = new LngLat(116.387271, 39.922501);
System.err.println(AMapUtils.calculateLineDistance(start, end));
}
}
运行结果为:1569.6213922679392,官网的javascript API示例结果如图:
结果虽然有一点误差,但是这hi在可接受范围内的。
Javascript实现
同样的算法,将其转换成JS的写法,完整的代码如下:
* 存储经纬度
* @param {Object} longitude
* @param {Object} latitude
*/
function LngLat(longitude, latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
function calculateLineDistance(start, end) {
var d1 = 0.01745329251994329;
var d2 = start.longitude;
var d3 = start.latitude;
var d4 = end.longitude;
var d5 = end.latitude;
d2 *= d1;
d3 *= d1;
d4 *= d1;
d5 *= d1;
var d6 = Math.sin(d2);
var d7 = Math.sin(d3);
var d8 = Math.cos(d2);
var d9 = Math.cos(d3);
var d10 = Math.sin(d4);
var d11 = Math.sin(d5);
var d12 = Math.cos(d4);
var d13 = Math.cos(d5);
var arrayOfDouble1 = [];
var arrayOfDouble2 = [];
arrayOfDouble1.push(d9 * d8);
arrayOfDouble1.push(d9 * d6);
arrayOfDouble1.push(d7);
arrayOfDouble2.push(d13 * d12);
arrayOfDouble2.push(d13 * d10);
arrayOfDouble2.push(d11);
var d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0]) +
(arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1]) +
(arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2]));
return(Math.asin(d14 / 2.0) * 12742001.579854401);
}
var start = new LngLat(116.368904, 39.923423);
var end = new LngLat(116.387271, 39.922501);
MySQL实现
DELIMITER $$
CREATE FUNCTION `calculateLineDistance`(startLng double, startLat double, endLng double, endLat double) RETURNS double
BEGIN
declare d2 DOUBLE;
declare d3 DOUBLE;
declare d4 DOUBLE;
declare d5 DOUBLE;
declare d6 DOUBLE;
declare d7 DOUBLE;
declare d8 DOUBLE;
declare d9 DOUBLE;
declare d10 DOUBLE;
declare d11 DOUBLE;
declare d12 DOUBLE;
declare d13 DOUBLE;
declare d14 DOUBLE;
declare arrayOfDouble10 DOUBLE;
declare arrayOfDouble11 DOUBLE;
declare arrayOfDouble12 DOUBLe;
declare arrayOfDouble20 DOUBLE;
declare arrayOfDouble21 DOUBLE;
declare arrayOfDouble22 DOUBLE;
set d2 = startLng * 0.01745329251994329;
set d3 = startLat * 0.01745329251994329;
set d4 = endLng * 0.01745329251994329;
set d5 = endLat * 0.01745329251994329;
set d6 = sin(d2);
set d7 = sin(d3);
set d8 = cos(d2);
set d9 = cos(d3);
set d10 = sin(d4);
set d11 = sin(d5);
set d12 = cos(d4);
set d13 = cos(d5);
set arrayOfDouble10 = (d9 * d8);
set arrayOfDouble11 = (d9 * d6);
set arrayOfDouble12 = d7;
set arrayOfDouble20 = (d13 * d12);
set arrayOfDouble21 = (d13 * d10);
set arrayOfDouble22 = d11;
set d14 = sqrt((arrayOfDouble10 - arrayOfDouble20) * (arrayOfDouble10 - arrayOfDouble20)
+ (arrayOfDouble11 - arrayOfDouble21) * (arrayOfDouble11 - arrayOfDouble21)
+ (arrayOfDouble12 - arrayOfDouble22) * (arrayOfDouble12 - arrayOfDouble22));
return (asin(d14 / 2.0) * 12742001.579854401);
END $$
DELIMITER ;
--------------------- 本文来自 蒋固金 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/jianggujin/article/details/72833711?utm_source=copy
-
C# 高德地图百度地图计算两点坐标距离
2018-09-30 00:22:48C# 高德地图百度地图计算两点坐标距离,返回两点之间的距离 -
php实现计算百度地图坐标之间距离的方法
2020-10-22 10:50:02主要介绍了php实现计算百度地图坐标之间距离的方法,涉及php字符串、数组及数学运算的相关技巧,需要的朋友可以参考下 -
计算两个地图坐标的直线距离
2016-11-24 17:04:27计算两个地图坐标的直线距离,计算两个地图坐标的直线距离 -
百度地图坐标距离计算,源于百度地图JS API 2.0
2016-10-30 05:24:09百度地图坐标距离计算,源于百度地图JS API 2.0前言开始没有考虑周全,一直按平面距离计算结果自然不正确,后来在网上找到一些计算方法还是跟百度官方得出的值有很大差异,看了百度API源码才知道,原来是这样计算...百度地图坐标距离计算,源于百度地图JS API 2.0
前言
开始没有考虑周全,一直按平面距离计算结果自然不正确,后来在网上找到一些计算方法还是跟百度官方得出的值有很大差异,看了百度API源码才知道,原来是这样计算球面距离的,不多说了,上代码!
JS精简版
function OD(a, b, c) { while (a > c) a -= c - b; while (a < b) a += c - b; return a; } function SD(a, b, c) { b != null && (a = Math.max(a, b)); c != null && (a = Math.min(a, c)); return a; } function getDistance(a_lat,a_lng,b_lat,b_lng) { var a = Math.PI * OD(a_lat, -180, 180) / 180; var b = Math.PI * OD(b_lat, -180, 180) / 180; var c = Math.PI * SD(a_lng, -74, 74) / 180; var d = Math.PI * SD(b_lng, -74, 74) / 180; return 6370996.81 * Math.acos(Math.sin(c) * Math.sin(d) + Math.cos(c) * Math.cos(d) * Math.cos(b-a)); } //使用并保留小数点后两位 var m =getDistance(106.486654,29.490295,106.581515,29.615467).toFixed(2); //获取到的单位是 米 alert(m);
JS 转 PHP版
function OD($a, $b, $c) { while ($a > $c) $a -= $c - $b; while ($a < $b) $a += $c - $b; return $a; } function SD($a, $b, $c) { $b != null && ($a = max($a, $b)); $c != null && ($a = min($a, $c)); return $a; } function getDistance($a_lat,$a_lng,$b_lat,$b_lng) { //由于php的pi() 与 js的Math.PI的差异,为保证和JS计算的值统一故直接使用近似值 $a = 3.141592653589793 * OD($a_lat, -180, 180) / 180; $b = 3.141592653589793 * OD($b_lat, -180, 180) / 180; $c = 3.141592653589793 * SD($a_lng, -74, 74) / 180; $d = 3.141592653589793 * SD($b_lng, -74, 74) / 180; return 6370996.81 * acos(sin($c) * sin($d) + cos($c) * cos($d) * cos($b-$a)); } //使用并保留小数点后两位 echo number_format(getDistance(106.486654,29.490295,106.581515,29.615467),2,'.','');
百度JS API 2.0算法完整版
//百度坐标距离计算,从压缩代码里扣出来的,故可读性差些,将就着还能看 var R = 6370996.81,//地球半径(米) p=null, j = void 0, Ib = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; function Jb(a) { var b = "", c, d, e = "", f, g = "", i = 0; f = /[^A-Za-z0-9\+\/\=]/g; if (!a || f.exec(a)) return a; a = a.replace(/[^A-Za-z0-9\+\/\=]/g, ""); do c = Ib.indexOf(a.charAt(i++)), d = Ib.indexOf(a.charAt(i++)), f = Ib.indexOf(a.charAt(i++)), g = Ib.indexOf(a.charAt(i++)), c = c << 2 | d >> 4, d = (d & 15) << 4 | f >> 2, e = (f & 3) << 6 | g, b += String.fromCharCode(c), 64 != f && (b += String.fromCharCode(d)), 64 != g && (b += String.fromCharCode(e)); while (i < a.length); return b; } function Za(a) { return "string" == typeof a; } function Point(a, b) { isNaN(a) && (a = Jb(a), a = isNaN(a) ? 0 : a); Za(a) && (a = parseFloat(a)); isNaN(b) && (b = Jb(b), b = isNaN(b) ? 0 : b); Za(b) && (b = parseFloat(b)); this.lng = a; this.lat = b } Point.prototype.mb = function (a) { return a && this.lat == a.lat && this.lng == a.lng }; function OD(a, b, c) { for (; a > c;) a -= c - b; for (; a < b;) a += c - b; return a; } function SD(a, b, c) { b != p && (a = Math.max(a, b)); c != p && (a = Math.min(a, c)); return a; } function Tk(a) { return Math.PI * a / 180; } function Pe(a, b, c, d) { return R * Math.acos(Math.sin(c) * Math.sin(d) + Math.cos(c) * Math.cos(d) * Math.cos(b - a)); } function Vo(a, b) { if (!a || !b) return 0; a.lng = OD(a.lng, -180, 180); a.lat = SD(a.lat, -74, 74); b.lng = OD(b.lng, -180, 180); b.lat = SD(b.lat, -74, 74); return Pe(Tk(a.lng),Tk(b.lng),Tk(a.lat),Tk(b.lat)) } function getDistance(a, b) { if (a && b) { if (a.mb(b)) return 0; var c; c = Vo(a, b); if (c === p || c === j) c = 0; return c; } } //使用并保留小数点后两位 var p1 = new Point(106.486654,29.490295); var p2 = new Point(106.581515,29.615467); var m = getDistance(p1,p2).toFixed(2); //获取到的单位是 米 alert(m);
注:都看到这了,点个赞吧 ^_^
-
高德地图计算两坐标之间距离
2017-06-01 17:48:29最近在做与地图相关的应用,使用了高德地图,研究了下高德地图计算两坐标距离的方法,官网上提供的开发包中有相关的方法,但是我的产品中比较特殊,无法直接使用提供的方法,所以就自己封装了相关计算方法,供大家...最近在做与地图相关的应用,使用了高德地图,研究了下高德地图计算两坐标距离的方法,官网上提供的开发包中有相关的方法,但是我的产品中比较特殊,无法直接使用提供的方法,所以就自己封装了相关计算方法,供大家参考。
Java实现
首先定义一个用于存储经纬度的类,这里起个名字叫:
LngLat
package amap; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; /** * 存储经纬度坐标值的类,单位角度 * * @author jianggujin * */ public final class LngLat implements Cloneable { /** * 纬度 (垂直方向) */ public final double latitude; /** * 经度 (水平方向) */ public final double longitude; /** * 格式化 */ private static DecimalFormat format = new DecimalFormat("0.000000", new DecimalFormatSymbols(Locale.US)); /** * 使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点。 * * @param longitude * 地点的经度,在-180 与180 之间的double 型数值。 * @param latitude * 地点的纬度,在-90 与90 之间的double 型数值。 */ public LngLat(double longitude, double latitude) { this(longitude, latitude, true); } /** * 使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点 * * @param longitude * 地点的经度,在-180 与180 之间的double 型数值。 * * @param latitude * 地点的纬度,在-90 与90 之间的double 型数值。 * @param isCheck * 是否需要检查经纬度的合理性,建议填写true */ public LngLat(double longitude, double latitude, boolean isCheck) { if (isCheck) { if ((-180.0D <= longitude) && (longitude < 180.0D)) this.longitude = parse(longitude); else { throw new IllegalArgumentException("the longitude range [-180, 180]."); // this.longitude = parse(((longitude - 180.0D) % 360.0D + 360.0D) % // 360.0D - 180.0D); } if ((latitude < -90.0D) || (latitude > 90.0D)) { throw new IllegalArgumentException("the latitude range [-90, 90]."); } this.latitude = latitude; // this.latitude = parse(Math.max(-90.0D, Math.min(90.0D, latitude))); } else { this.latitude = latitude; this.longitude = longitude; } } /** * 解析 * * @param d * @return */ private static double parse(double d) { return Double.parseDouble(format.format(d)); } public LngLat clone() { return new LngLat(this.latitude, this.longitude); } @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(latitude); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(longitude); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; LngLat other = (LngLat) obj; if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude)) return false; if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude)) return false; return true; } public String toString() { return "lat/lng: (" + this.latitude + "," + this.longitude + ")"; } }
计算工具类如下:
package amap; /** * 高德地图工具 * * @author jianggujin * */ public class AMapUtils { /** * 根据用户的起点和终点经纬度计算两点间距离,此距离为相对较短的距离,单位米。 * * @param start * 起点的坐标 * @param end * 终点的坐标 * @return */ public static double calculateLineDistance(LngLat start, LngLat end) { if ((start == null) || (end == null)) { throw new IllegalArgumentException("非法坐标值,不能为null"); } double d1 = 0.01745329251994329D; double d2 = start.longitude; double d3 = start.latitude; double d4 = end.longitude; double d5 = end.latitude; d2 *= d1; d3 *= d1; d4 *= d1; d5 *= d1; double d6 = Math.sin(d2); double d7 = Math.sin(d3); double d8 = Math.cos(d2); double d9 = Math.cos(d3); double d10 = Math.sin(d4); double d11 = Math.sin(d5); double d12 = Math.cos(d4); double d13 = Math.cos(d5); double[] arrayOfDouble1 = new double[3]; double[] arrayOfDouble2 = new double[3]; arrayOfDouble1[0] = (d9 * d8); arrayOfDouble1[1] = (d9 * d6); arrayOfDouble1[2] = d7; arrayOfDouble2[0] = (d13 * d12); arrayOfDouble2[1] = (d13 * d10); arrayOfDouble2[2] = d11; double d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0]) + (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1]) + (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2])); return (Math.asin(d14 / 2.0D) * 12742001.579854401D); } }
最后边写一段测试代码测试一下:
package test; import org.junit.Test; import amap.AMapUtils; import amap.LngLat; public class AMapTest { @Test public void Test() { LngLat start = new LngLat(116.368904, 39.923423); LngLat end = new LngLat(116.387271, 39.922501); System.err.println(AMapUtils.calculateLineDistance(start, end)); } }
运行结果为:
1569.6213922679392
,官网的javascript API示例结果如图:结果虽然有一点误差,但是这hi在可接受范围内的。
Javascript实现
同样的算法,将其转换成
JS
的写法,完整的代码如下:<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script type="text/javascript" src="js/ajax.js"></script> <script> /** * 存储经纬度 * @param {Object} longitude * @param {Object} latitude */ function LngLat(longitude, latitude) { this.longitude = longitude; this.latitude = latitude; } function calculateLineDistance(start, end) { var d1 = 0.01745329251994329; var d2 = start.longitude; var d3 = start.latitude; var d4 = end.longitude; var d5 = end.latitude; d2 *= d1; d3 *= d1; d4 *= d1; d5 *= d1; var d6 = Math.sin(d2); var d7 = Math.sin(d3); var d8 = Math.cos(d2); var d9 = Math.cos(d3); var d10 = Math.sin(d4); var d11 = Math.sin(d5); var d12 = Math.cos(d4); var d13 = Math.cos(d5); var arrayOfDouble1 = []; var arrayOfDouble2 = []; arrayOfDouble1.push(d9 * d8); arrayOfDouble1.push(d9 * d6); arrayOfDouble1.push(d7); arrayOfDouble2.push(d13 * d12); arrayOfDouble2.push(d13 * d10); arrayOfDouble2.push(d11); var d14 = Math.sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) * (arrayOfDouble1[0] - arrayOfDouble2[0]) + (arrayOfDouble1[1] - arrayOfDouble2[1]) * (arrayOfDouble1[1] - arrayOfDouble2[1]) + (arrayOfDouble1[2] - arrayOfDouble2[2]) * (arrayOfDouble1[2] - arrayOfDouble2[2])); return(Math.asin(d14 / 2.0) * 12742001.579854401); } var start = new LngLat(116.368904, 39.923423); var end = new LngLat(116.387271, 39.922501); </script> </head> <body> <script> document.write(calculateLineDistance(start, end)); </script> </body> </html>
MySQL实现
DELIMITER $$ CREATE FUNCTION `calculateLineDistance`(startLng double, startLat double, endLng double, endLat double) RETURNS double BEGIN declare d2 DOUBLE; declare d3 DOUBLE; declare d4 DOUBLE; declare d5 DOUBLE; declare d6 DOUBLE; declare d7 DOUBLE; declare d8 DOUBLE; declare d9 DOUBLE; declare d10 DOUBLE; declare d11 DOUBLE; declare d12 DOUBLE; declare d13 DOUBLE; declare d14 DOUBLE; declare arrayOfDouble10 DOUBLE; declare arrayOfDouble11 DOUBLE; declare arrayOfDouble12 DOUBLe; declare arrayOfDouble20 DOUBLE; declare arrayOfDouble21 DOUBLE; declare arrayOfDouble22 DOUBLE; set d2 = startLng * 0.01745329251994329; set d3 = startLat * 0.01745329251994329; set d4 = endLng * 0.01745329251994329; set d5 = endLat * 0.01745329251994329; set d6 = sin(d2); set d7 = sin(d3); set d8 = cos(d2); set d9 = cos(d3); set d10 = sin(d4); set d11 = sin(d5); set d12 = cos(d4); set d13 = cos(d5); set arrayOfDouble10 = (d9 * d8); set arrayOfDouble11 = (d9 * d6); set arrayOfDouble12 = d7; set arrayOfDouble20 = (d13 * d12); set arrayOfDouble21 = (d13 * d10); set arrayOfDouble22 = d11; set d14 = sqrt((arrayOfDouble10 - arrayOfDouble20) * (arrayOfDouble10 - arrayOfDouble20) + (arrayOfDouble11 - arrayOfDouble21) * (arrayOfDouble11 - arrayOfDouble21) + (arrayOfDouble12 - arrayOfDouble22) * (arrayOfDouble12 - arrayOfDouble22)); return (asin(d14 / 2.0) * 12742001.579854401); END $$ DELIMITER ;
-
php mysql计算距离_php实现计算百度地图坐标之间距离的方法
2021-03-04 08:16:20分享给大家供大家参考,具体如下:下面是网上的代码,使用的时候需要进行些许修改第一个函数是获得范围,参数为纬度经度半径第二个函数是计算坐标距离define('PI',3.1415926535898);define('EARTH_RADIUS',6378.137);/...本文实例讲述了php实现计算百度地图坐标之间距离的方法。分享给大家供大家参考,具体如下:
下面是网上的代码,使用的时候需要进行些许修改
第一个函数是获得范围,参数为纬度经度半径
第二个函数是计算坐标距离
define('PI',3.1415926535898);
define('EARTH_RADIUS',6378.137);
//计算范围,可以做搜索用户
function GetRange($lat,$lon,$raidus){
//计算纬度
$degree = (24901 * 1609) / 360.0;
$dpmLat = 1 / $degree;
$radiusLat = $dpmLat * $raidus;
$minLat = $lat - $radiusLat; //得到最小纬度
$maxLat = $lat + $radiusLat; //得到最大纬度
//计算经度
$mpdLng = $degree * cos($lat * (PI / 180));
$dpmLng = 1 / $mpdLng;
$radiusLng = $dpmLng * $raidus;
$minLng = $lon - $radiusLng; //得到最小经度
$maxLng = $lon + $radiusLng; //得到最大经度
//范围
$range = array(
'minLat' => $minLat,
'maxLat' => $maxLat,
'minLon' => $minLng,
'maxLon' => $maxLng
);
return $range;
}
//获取2点之间的距离
function GetDistance($lat1, $lng1, $lat2, $lng2){
$radLat1 = $lat1 * (PI / 180);
$radLat2 = $lat2 * (PI / 180);
$a = $radLat1 - $radLat2;
$b = ($lng1 * (PI / 180)) - ($lng2 * (PI / 180));
$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)));
$s = $s * EARTH_RADIUS;
$s = round($s * 10000) / 10000;
return $s;
}
希望本文所述对大家PHP程序设计有所帮助。
-
thinkphp 百度地图Api坐标计算 A坐标距离B坐标多少公里 并按照距离近的排序 坐标排序 外部字段排序...
2019-10-06 10:55:54//计算距离 /* **$a 可多数坐标 就是可数组类型的 ***$b 是登录者的坐标 ***ps: lat经度 lng纬度 经度在前纬度在后 *** ***/ function juli($a, $b) { // $key[0] = '3uF44dvwWrW7S9GLgBPk3CVh'; // $key... -
高德地图获取坐标距离_利用java、js或mysql计算高德地图中两坐标之间的距离
2021-01-17 19:27:29前言因为工作的原因,最近在做与地图相关的应用,使用了高德地图,研究了下高德地图计算两坐标距离的方法,官网上提供的开发包中有相关的方法,但是我的产品中比较特殊,无法直接使用提供的方法,所以就自己封装了... -
百度地图与谷歌地图坐标转换,WGS84、GCJ02、BD09地图坐标系间的坐标转换及坐标距离计算
2017-05-05 17:52:39然而一些地图SDK给出的地图坐标转换接口的实现是在服务器进行的,那么这些接口的调用不但会发起网络请求而效率上也会非常的慢,无论是对于客户端离线操作还是在自己的服务后台批量转换坐标数据,都需 -
百度地图坐标间距离 通过java计算
2019-07-04 17:00:02原因:前端用的百度地图,数据库存的百度地图坐标,需要在后台(java)计算两点坐标的直线距离,网上的一些通用方法计算出来的和使用百度js api得到的结果不一样,无奈只有阅读源码,再用java写一遍。 先看百度js api... -
高德地图获取坐标距离_计算两个坐标点之间的距离(高德地图)
2021-01-17 19:27:31/*** 计算两点的距离** @param fromPoint* @param toPoint* @return 返回String类型带距离单位*/public static String measureDistanceStr(LatLng fromPoint, LatLng toPoint) {String distanceStr = "";if (from... -
WGS84、GCJ02、BD09地图坐标系间的坐标转换及坐标距离计算
2020-04-29 11:08:59坐标转换转载来源:...设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,最基础的坐标,谷歌地图在非中国地区使用的坐标系 GPS/谷歌地图卫星 GCJ02 火星坐标系,是由中国国...