Android 获取两个地理点之间的距离

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2741403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:10:55  来源:igfitidea点击:

Get the distance between two geo points

androidgeolocationmaps

提问by Chmouel Boudjnah

I want to make an app which checks the nearest place where a user is. I can easily get the location of the user and I have already a list of places with latitude and longitude.

我想制作一个应用程序来检查用户最近的位置。我可以轻松获取用户的位置,并且我已经有了一个包含经纬度的地点列表。

What would be the best way to know the nearest place of the list against the current user location.

了解与当前用户位置最近的列表位置的最佳方法是什么。

I could not find anything in the google APIs.

我在谷歌 API 中找不到任何东西。

回答by praveen

Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);

Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);

float distanceInMeters = loc1.distanceTo(loc2);

Reference: http://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)

参考:http: //developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)

回答by haseman

http://developer.android.com/reference/android/location/Location.html

http://developer.android.com/reference/android/location/Location.html

Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:

查看 distanceTo 或 distanceBetween。您可以从纬度和经度创建 Location 对象:

Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);

回答by Laurent Grégtheitroade

An approximated solution (based on an equirectangular projection), much faster(it requires only 1 trig and 1 square root).

近似解(基于等距柱状投影),速度要快得多(它只需要 1 个三角函数和 1 个平方根)。

This approximation is relevant if your points are not too far apart. It will always over-estimatecompared to the real haversine distance. For example it will add no more than 0.05382 %to the real distance if the delta latitude or longitude between your two points does not exceed 4 decimal degrees.

如果您的点相距不太远,则此近似值是相关的。与实际半正弦距离相比,它总是高估。例如,如果两点之间的纬度或经度增量不超过 4 个十进制度数,则实际距离增加不超过 0.05382%

The standard formula (Haversine) is the exactone (that is, it works for any couple of longitude/latitude on earth) but is much sloweras it needs 7 trigonometric and 2 square roots. If your couple of points are not too far apart, and absolute precision is not paramount, you can use this approximate version (Equirectangular), which is much faster as it uses only one trigonometric and one square root.

标准公式 (Haversine) 是精确公式(即,它适用于地球上任意一对经度/纬度),但速度要慢得多,因为它需要 7 个三角函数和 2 个平方根。如果您的两个点相距不太远,并且绝对精度不是最重要的,您可以使用这个近似版本 (Equirectangular),因为它只使用一个三角函数和一个平方根,所以速度要快得多。

// Approximate Equirectangular -- works if (lat1,lon1) ~ (lat2,lon2)
int R = 6371; // km
double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
double y = (lat2 - lat1);
double distance = Math.sqrt(x * x + y * y) * R;

You can optimize this furtherby either:

您可以通过以下任一方式进一步优化

  1. Removing the square rootif you simply compare the distance to another (in that case compare both squared distance);
  2. Factoring-out the cosineif you compute the distance from one master point to many others (in that case you do the equirectangular projection centered on the master point, so you can compute the cosine once for all comparisons).
  1. 如果您只是将距离与另一个距离进行比较,则删除平方根(在这种情况下,比较两个平方距离);
  2. 保理业务时的余弦,如果你计算从一个主点到许多其他的距离(在这种情况下,你做集中在主点的方形投影,这样你就可以计算余弦一旦所有的比较)。

For more info see: http://www.movable-type.co.uk/scripts/latlong.html

有关更多信息,请参阅:http: //www.movable-type.co.uk/scripts/latlong.html

There is a nice reference implementation of the Haversine formula in several languages at: http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

有几种语言的Haversine公式的很好的参考实现:http: //www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

回答by Dwaine Bailey

There are a couple of methods you could use, but to determine which one is best we first need to know if you are aware of the user's altitude, as well as the altitude of the other points?

您可以使用多种方法,但要确定哪种方法最好,我们首先需要知道您是否知道用户的高度以及其他点的高度?

Depending on the level of accuracy you are after, you could look into either the Haversine or Vincenty formulae...

根据您所追求的准确度水平,您可以查看Haversine 或Vincenty 公式...

These pages detail the formulae, and, for the less mathematically inclined also provide an explanation of how to implement them in script!

这些页面详细介绍了公式,对于不太喜欢数学的人,还提供了如何在脚本中实现它们的解释!

Haversine Formula: http://www.movable-type.co.uk/scripts/latlong.html

半正弦公式:http: //www.movable-type.co.uk/scripts/latlong.html

Vincenty Formula: http://www.movable-type.co.uk/scripts/latlong-vincenty.html

文森蒂公式:http: //www.movable-type.co.uk/scripts/latlong-vincenty.html

If you have any problems with any of the meanings in the formulae, just comment and I'll do my best to answer them :)

如果您对公式中的任何含义有任何疑问,请发表评论,我会尽力回答:)

回答by Jawad Zeb

There are two ways to get distance between LatLng.

有两种方法可以获得 LatLng 之间的距离。

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

See this

看到这个

and second

第二个

public float distanceTo (Location dest)as answered by praveen.

public float distanceTo (Location dest)正如praveen所回答的那样。

回答by Levon Petrosyan

private float getDistance(double lat1, double lon1, double lat2, double lon2) {
        float[] distance = new float[2];
        Location.distanceBetween(lat1, lon1, lat2, lon2, distance);
        return distance[0];
    }

回答by farhad.kargaran

Just use the following method, pass it lat and long and get distance in meter:

只需使用以下方法,通过纬度和经度并以米为单位获取距离:

private static double distance_in_meter(final double lat1, final double lon1, final double lat2, final double lon2) {
    double R = 6371000f; // Radius of the earth in m
    double dLat = (lat1 - lat2) * Math.PI / 180f;
    double dLon = (lon1 - lon2) * Math.PI / 180f;
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(latlong1.latitude * Math.PI / 180f) * Math.cos(latlong2.latitude * Math.PI / 180f) *
                    Math.sin(dLon/2) * Math.sin(dLon/2);
    double c = 2f * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double d = R * c;
    return d;
}

回答by Sai Gopi N

you can get distance and time using google Map API Google Map API

您可以使用 google Map API Google Map API获取距离和时间

just pass downloaded JSON to this method you will get real time Distance and Time between two latlong's

只需将下载的 JSON 传递给此方法,您将获得两个 latlong 之间的实时距离和时间

void parseJSONForDurationAndKMS(String json) throws JSONException {

    Log.d(TAG, "called parseJSONForDurationAndKMS");
    JSONObject jsonObject = new JSONObject(json);
    String distance;
    String duration;
    distance = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getString("text");
    duration = jsonObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("duration").getString("text");

    Log.d(TAG, "distance : " + distance);
    Log.d(TAG, "duration : " + duration);

    distanceBWLats.setText("Distance : " + distance + "\n" + "Duration : " + duration);


}