当您知道java中的经度和纬度时,以米为单位计算距离 [英] Calculate distance in meters when you know longitude and latitude in java

查看:47
本文介绍了当您知道java中的经度和纬度时,以米为单位计算距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
在 Java 中使用纬度/经度值

重复:

  • 在 Java 中处理纬度/经度值
  • 如何计算两个纬度经度点之间的距离?
  • 我需要计算由两个坐标给出的两点之间的距离.我在做的项目是Java项目,所以Java-code会很好,但是也可以给出伪代码,那我自己实现吧:)

    I need to calculate the distance between two points given by two coordinates. The project I am working on is a Java-project, so Java-code will be great, but pseudo-code can also be given, then I can implement it myself :)

    您可能知道,坐标有三种表示方式:

    As you probably know, there are three ways to represent coordinates:

    • 度数:分:秒(49°30'00"N,123°30'00"W)
    • 度数:十进制分 (49°30.0', -123°30.0'), (49d30.0m,-123d30.0')
    • 十进制度数(49.5000°,-123.5000°),通常有 4-6 个十进制数.

    这是我的坐标的第三种方式,因此该值的代码将是首选:)

    It's the third way my coordinates are given in, so the code for this values will be preferred :)

    推荐答案

    基于关于stackoverflow的另一个问题,我得到了这个代码..这以米为单位计算结果,而不是以英里为单位:)

    Based on another question on stackoverflow, I got this code.. This calculates the result in meters, not in miles :)

     public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
        double earthRadius = 6371000; //meters
        double dLat = Math.toRadians(lat2-lat1);
        double dLng = Math.toRadians(lng2-lng1);
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                   Math.sin(dLng/2) * Math.sin(dLng/2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        float dist = (float) (earthRadius * c);
    
        return dist;
        }
    

    这篇关于当您知道java中的经度和纬度时,以米为单位计算距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆