Android 使用高德地图计算两坐标间的距离

两个工具类:

第一个:

package com.dyzh.ibroker.util;

/**
 * Created by Administrator on 2018/6/20 0020.
 */

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public 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 com.dyzh.ibroker.util;

/**
 * Created by Administrator on 2018/6/20 0020.
 */

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);
    }
}
简单使用:
//这个经纬度是我存到本地的
LngLat start = new LngLat(Double.parseDouble(SharedPreferencesUtil.getinstance(ChatLocationAddress.this).getString(SharedPreferencesUtil.CHATSLONGITUDE)), Double.parseDouble(SharedPreferencesUtil.getinstance(ChatLocationAddress.this).getString(SharedPreferencesUtil.CHATSLATITUDE)));
LngLat end = new LngLat(Double.parseDouble(SharedPreferencesUtil.getinstance(ChatLocationAddress.this).getString(SharedPreferencesUtil.MINELON)), Double.parseDouble(SharedPreferencesUtil.getinstance(ChatLocationAddress.this).getString(SharedPreferencesUtil.MINELAT)));
//这样就可以打印出来我们想要的距离是多少米了
Log.e("=========", String.valueOf(AMapUtils.calculateLineDistance(start, end)));

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值