【问题标题】:Distance between geopoints地理点之间的距离
【发布时间】:2012-01-04 10:27:29
【问题描述】:

我在计算两个地理点之间的距离时遇到问题。

地理点是:

position1 = mapView.getProjection().fromPixels(
(int) e.getX(),
(int) e.getY());

还有一个

double lat = 35.1064;
double lng = 22.556412;
GeoPoint position2 = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));

然后我创建两个位置:

Location loc = new Location("");                                

loc.setLatitude(position1.getLatitudeE6());

loc.setLongitude(position1.getLongitudeE6());

Location loc2 = new Location("");                               

loc.setLatitude(position2.getLatitudeE6());

loc.setLongitude(position2.getLongitudeE6());

然后我计算距离:

float distance = loc.distanceTo(loc2);

我把它圆了:

Math.round(distance);

但我得到的结果如下:

1.4331783E7

我做错了什么?

【问题讨论】:

  • 查看此链接codecodex.com/wiki/…
  • 为什么你认为 1.4331783E7 是错误的?
  • 因为距离应该以米为单位,而我设置和点击的点最大为 100 米,所以我认为这不是我需要的结果!

标签: android google-maps


【解决方案1】:

尝试按照我的方法,

    /**
 * 
 * @param lat1 Latitude of the First Location
 * @param lng1 Logitude of the First Location
 * @param lat2 Latitude of the Second Location
 * @param lng2 Longitude of the Second Location
 * @return distance between two lat-lon in float format
 */

public static float distFrom (float lat1, float lng1, float lat2, float lng2 ) 
{
    double earthRadius = 3958.75;
    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));
    double dist = earthRadius * c;

    int meterConversion = 1609;

    return new Float(dist * meterConversion).floatValue();
}

【讨论】:

    【解决方案2】:

    你选错了经纬度,请替换下面几行

    Location loc = new Location("");                                
    
    loc.setLatitude(position1.getLatitudeE6());
    
    loc.setLongitude(position1.getLongitudeE6());
    
    Location loc2 = new Location("");                               
    
    loc.setLatitude(position2.getLatitudeE6());
    
    loc.setLongitude(position2.getLongitudeE6());
    

    Location loc = new Location("");                                
    
    loc.setLatitude(position1.getLatitudeE6()/1E6);
    
    loc.setLongitude(position1.getLongitudeE6()/1E6);
    
    Location loc2 = new Location("");                               
    
    loc.setLatitude(position2.getLatitudeE6()/1E6);
    
    loc.setLongitude(position2.getLongitudeE6()/1E6);
    

    然后一次,你会得到正确的答案。

    【讨论】:

      【解决方案3】:

      您将 GeoPoint 值设置为 Location 对象,而不是双精度值尝试设置为纬度和经度的双精度值

      Location loc = new Location("");                                
      
      loc.setLatitude(35.1064);
      
      loc.setLongitude(22.556412);
      
      Location loc2 = new Location("");                               
      
      loc2.setLatitude(22.556412);
      
      loc2.setLongitude(35.1064);
      

      现在获取位置

      float distance = loc.distanceTo(loc2);
      

      【讨论】:

      • 我不能将这两个值都设置为纯数字,因为第一个地理点是根据用户点击的地图位置生成的。
      • 无论我只是通过以双精度格式传递值来给出示例,您都可以通过使用 1e6 值(例如 loc.setLatitude(position1.getLatitudeE6()/1E6) 和另一个值来划分它们来传递地理点值)问题是您没有为 loc2 对象设置 lat/lng 值,您只是更改了 loc 值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      相关资源
      最近更新 更多