如何在地图中找到两个地理位置之间的正确距离?

Suj*_*anj 5 android android-location

我需要开发应用程序,用户必须找到他停放的汽车,并显示他和停车之间的距离.我使用GPS和定位服务.

对于距离我用半正矢式但距离始终显示0米.

我尝试了很多搜索谷歌的解决方案,但dint得到任何正确的解决方案.

谁能提出他们的建议?

Vip*_*hah 12

Google文档有两种方法

在此输入图像描述

如果你从GeoPoint获得lat/lon,那么他们是微观的.你必须乘以1e6.

但我更喜欢使用以下方法.(基于Haversine Formula)

http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

double dist = GeoUtils.distanceKm(mylat, mylon, lat, lon);

 /**
 * Computes the distance in kilometers between two points on Earth.
 * 
 * @param lat1 Latitude of the first point
 * @param lon1 Longitude of the first point
 * @param lat2 Latitude of the second point
 * @param lon2 Longitude of the second point
 * @return Distance between the two points in kilometers.
 */

public static double distanceKm(double lat1, double lon1, double lat2, double lon2) {
    int EARTH_RADIUS_KM = 6371;
    double lat1Rad = Math.toRadians(lat1);
    double lat2Rad = Math.toRadians(lat2);
    double deltaLonRad = Math.toRadians(lon2 - lon1);

    return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)) * EARTH_RADIUS_KM;
}
Run Code Online (Sandbox Code Playgroud)

最后我想分享奖金信息.

如果您正在寻找行车路线,请在两个地点之间路线前往

http://code.google.com/p/j2memaprouteprovider/


Nik*_*iko 1

android.location.Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

地理点有 getLongitudeE6() 和 getLatitudeE6() 来提供帮助。请记住,这些是 E6,因此您需要除以 1E6。


归档时间:

查看次数:

5364 次

最近记录:

12 年,12 月 前