【问题标题】:Source code to find the distance between two geo points查找两个地理点之间距离的源代码
【发布时间】:2011-12-20 03:23:07
【问题描述】:

我正在开发一个 Android 应用程序,我想添加商店定位器详细信息和地图视图。我搜索了很多。但我没有得到任何有意义的东西。我很紧张,非常需要你的帮助。

问题是,我想求2个坐标之间的距离,我得到了一个Haversine公式,但是不知道如何成功运行程序。,由于我是Android初学者,请帮助逐步进行编码。即xml代码也...,请。,请。,

【问题讨论】:

标签: android gps haversine


【解决方案1】:

如上所述,位置类是要走的路。

这是我使用的代码:

Location locationA = new Location("point A");  

locationA.setLatitude(pointA.getLatitudeE6() / 1E6);  
locationA.setLongitude(pointB.getLongitudeE6() / 1E6);  

Location locationB = new Location("point B");  

locationB.setLatitude(pointB.getLatitudeE6() / 1E6);  
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);  

double distance = locationA.distanceTo(locationB);

在本例中,pointApointB 都是 GeoPoint 类的实例。

【讨论】:

    【解决方案2】:

    来自 MotoDev sn-ps(GPS 坐标之间的距离):

    Location originLocation = new Location("gps");
    Location destinationLocation = new Location("gps");
    originLocation.setLatitude(originLatitude);
    originLocation.setLongitude(originLongitude);
    destinationLocation.setLatitude(originLatitude);
    destinationLocation.setLongitude(originLongitude);
    float distance = originLocation.distanceTo(destinationLocation);
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

        使用此代码。你会得到两个地理点之间的距离。

         private String distance(double lati, double longi, double curlati1,
                double curlongi1) {
        
            double theta = longi - curlongi1;
            double dist = Math.sin(deg2rad(lati)) * Math.sin(deg2rad(curlati1))
                    + Math.cos(deg2rad(lati)) * Math.cos(deg2rad(curlati1))
                    * Math.cos(deg2rad(theta));
            dist = Math.acos(dist);
            dist = rad2deg(dist);
            dist = dist * 60 * 1.1515;
        
            dist = dist * 1.609344;
        
            result = Double.toString(dist);
            System.out.println("dist_result :" + result);
            return (result);
        }
        
        /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
        /* :: This function converts decimal degrees to radians : */
        /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
        private double deg2rad(double deg) {
            return (deg * Math.PI / 180.0);
        }
        
        /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
        /* :: This function converts radians to decimal degrees : */
        /* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
        private double rad2deg(double rad) {
            return (rad * 180.0 / Math.PI);
        }
        

        【讨论】:

          猜你喜欢
          • 2013-03-20
          • 1970-01-01
          • 1970-01-01
          • 2019-09-16
          • 2013-07-15
          • 1970-01-01
          • 2011-02-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多