简体   繁体   English

使用GPS查找两个Android设备之间的距离

[英]Find distance between two Android devices using GPS

is it possible that I can get the coordinates of another phone when that phone has no WIFI on? 当手机没有WIFI时,我可以获得另一部手机的坐标吗? I want to calculate the distance between two phones but none of the phones is connected on wifi. 我想计算两部手机之间的距离,但没有一部手机在wifi上连接。 Is this possible? 这可能吗?

you can get distance between two latitude and Longitude So if you are able to get latitude and longitude between two device then you can get the distance using following code 您可以获得两个纬度和经度之间的距离因此,如果您能够获得两个设备之间的纬度和经度,那么您可以使用以下代码获得距离

public static float calculateDistance(float lat1, float lon1, float lat2, float lon2)
    {
        float dLat = (float) Math.toRadians(lat2 - lat1);
        float dLon = (float) Math.toRadians(lon2 - lon1);
        float a =
                (float) (Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
                        * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2));
        float c = (float) (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
        float d = earthRadius * c;
        return d;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM