最近因为工作的需求,需要计算出高德地图中两个坐标的距离,通过查找相关资料发现了多种实现的方法,下面这篇文章主要给大家介绍了关于利用java、js或mysql计算高德地图中两坐标之间距离的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
前言
因为工作的原因,最近在做与地图相关的应用,使用了高德地图,研究了下高德地图计算两坐标距离的方法,官网上提供的开发包中有相关的方法,但是我的产品中比较特殊,无法直接使用提供的方法,所以就自己封装了相关计算方法,供大家参考,下面话不多说了,来一起看看详细的介绍吧。
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 la<i>本文来源gaodai$ma#com搞$$代**码)网@</i>titude) { 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 + ")"; }}