GPS距离计算

发布于 2024-12-20 22:59:04 字数 1094 浏览 2 评论 0原文

我想计算两个 GPS 位置之间的距离,每个位置都有一个纬度值和一个经度值。对于短距离结果,计算应该是准确的。例如。 < 300m。如果我使用 Google Earth(请参阅我的代码中的坐标),距离约为 136m。 如果我使用文章提供的解决方案: http://www.movable-type。 co.uk/scripts/latlong.html(半正矢公式)结果与此相差甚远。

使用的代码:

public void GpsCalc(){
    double d = getDistance(51.342299,4.371359, 51.342490,4.371997); 
    Log.e("GpsCalc", String.valueOf(d));
}

public static double getDistance(double lat1, double lng1, double lat2, double lng2){
    double R = 6371; // earth’s radius (mean radius = 6,371km)
    double dLat =  Math.toRadians(lat2-lat1);

    double dLon =  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(dLon/2) * Math.sin(dLon/2); 
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    double dr1 = R * c;//in radians      

    Log.e("getDistance-dr1", String.valueOf(dr1));

    return dr1;
}

我确信它应该是一些小的变化,但我看不到它。

I want to calculate the distance between two GPS locations, each with a latitute value and a longitude value. The calculations should be accurate for short-distance results. eg. < 300m. If I use Google Earth (see coord in my code) , the distance is ~136m.
If I use the solution provided by article: http://www.movable-type.co.uk/scripts/latlong.html (the haversine formula) the result is nothing near that.

used code:

public void GpsCalc(){
    double d = getDistance(51.342299,4.371359, 51.342490,4.371997); 
    Log.e("GpsCalc", String.valueOf(d));
}

public static double getDistance(double lat1, double lng1, double lat2, double lng2){
    double R = 6371; // earth’s radius (mean radius = 6,371km)
    double dLat =  Math.toRadians(lat2-lat1);

    double dLon =  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(dLon/2) * Math.sin(dLon/2); 
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    double dr1 = R * c;//in radians      

    Log.e("getDistance-dr1", String.valueOf(dr1));

    return dr1;
}

I'm sure it should be some minor change, but i can't see it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-12-27 22:59:04

我对该页面上的半正矢公式也遇到了麻烦。我知道这并不完全是你问题的答案,但我在余弦定律公式上取得了更大的成功,它给出了与谷歌地球相同的结果。如果它有帮助,它看起来像这样:

public double getDistance(double lat1, double lon1, double lat2, double lon2) {
    double latA = Math.toRadians(lat1);
    double lonA = Math.toRadians(lon1);
    double latB = Math.toRadians(lat2);
    double lonB = Math.toRadians(lon2);
    double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
                    (Math.sin(latA) * Math.sin(latB));
    double ang = Math.acos(cosAng);
    double dist = ang * EARTH_RADIUS;
    return dist;
}

编辑:

我在 Google 地图和 Google 地球以及我的代码中尝试了您的坐标,我得到了 49m 的坐标。也许从来就没有问题?
屏幕截图

I have also had trouble with the haversine formula on that page. I know it's not precisely an answer to your question, but I had more success with the law of cosines formula, which gives the same results as Google Earth. In case it helps, it looked like this:

public double getDistance(double lat1, double lon1, double lat2, double lon2) {
    double latA = Math.toRadians(lat1);
    double lonA = Math.toRadians(lon1);
    double latB = Math.toRadians(lat2);
    double lonB = Math.toRadians(lon2);
    double cosAng = (Math.cos(latA) * Math.cos(latB) * Math.cos(lonB-lonA)) +
                    (Math.sin(latA) * Math.sin(latB));
    double ang = Math.acos(cosAng);
    double dist = ang * EARTH_RADIUS;
    return dist;
}

EDIT:

I tried your coordinates in Google Maps and Google Earth and in my code, and I'm getting 49m for all of them. Maybe there was never a problem?
screenshot

茶底世界 2024-12-27 22:59:04

有点晚了,但还有两个选择

  1. 使用 Apple Corelocation(49.2733 米)

    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:_posGPSCurrent.latitude 经度:_posGPSCurrent.longitude];
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:_posGPSTarget.latitude 经度:_posGPSTarget.longitude];
    
    // 以公里为单位
    CLLocationDistance距离=[当前位置距离位置:位置]/1000;
    
  2. 手动计算(49.1393米)

包含数学库

#include <math.h>

// distance in Kilometers (Haversine)

-(double)distanceFromGPSlat1:(double)tlat1 lon2:(double)tlon1 lat2:(double)tlat2 lon2:(double)tlon2
{
double distance = ((acos(sin(tlat1*M_PI/180)*sin(tlat2*M_PI/180)+cos(tlat1*M_PI/180)*cos(tlat2*M_PI/180)*cos((tlon1-tlon2)*M_PI/180))*180/M_PI)*60*1.1515*1.609344);

return distance;
}

也许我更喜欢第二种方法(手动计算)。

A bit late, but two more options

  1. Using Apple Corelocation (49.2733 meters)

    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:_posGPSCurrent.latitude longitude:_posGPSCurrent.longitude];
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:_posGPSTarget.latitude longitude:_posGPSTarget.longitude];
    
    // in kilometers
    CLLocationDistance distance =[currentLocation distanceFromLocation:location]/1000;
    
  2. Manual calculation (49.1393 meters)

Include Math library

#include <math.h>

// distance in Kilometers (Haversine)

-(double)distanceFromGPSlat1:(double)tlat1 lon2:(double)tlon1 lat2:(double)tlat2 lon2:(double)tlon2
{
double distance = ((acos(sin(tlat1*M_PI/180)*sin(tlat2*M_PI/180)+cos(tlat1*M_PI/180)*cos(tlat2*M_PI/180)*cos((tlon1-tlon2)*M_PI/180))*180/M_PI)*60*1.1515*1.609344);

return distance;
}

Perhaps I prefer the second method (Manual calculation).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文