简体   繁体   English

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

[英]how to find the correct distance between two geopoints in map?

I need to develop app where user has to locate his car that he has parked and show distance between him and car parked.I used GPS and location services. 我需要开发应用程序,用户必须找到他停放的汽车并显示他和停车之间的距离。我使用GPS和定位服务。

For distance i used haversine formula but the distance always shows 0 meters. 对于距离我用haversine公式但距离始终显示0米。

I tried a lot searching for solution in google but dint get any correct solution. 我尝试了很多搜索谷歌的解决方案,但dint得到任何正确的解决方案。

Can anyone give their suggestions? 谁能提出他们的建议?

Google Docs have two methods Google文档有两种方法

在此输入图像描述

If you are getting lat/lon from GeoPoint then they are in microdegrees. 如果你从GeoPoint获得lat / lon,那么他们是微观的。 You must multiply by 1e6. 你必须乘以1e6。

But i preferred to use below method. 但我更喜欢使用以下方法。 (its based on Haversine Formula) (基于Haversine Formula)

http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe 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;
}

At last i would like to share bonus information. 最后我想分享奖金信息。

If you are looking for driving directions, route between two locations then head to 如果您正在寻找行车路线,请在两个地点之间路线前往

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

Try using This method in android.location API 尝试在android.location API中使用This方法

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

This method computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them 该方法计算两个位置之间以米为单位的近似距离,并且可选地计算它们之间的最短路径的初始和最终方位

NB: If you are getting lat/lon from GeoPoint then they are in microdegrees. 注意:如果你从GeoPoint获得lat / lon,那么他们是微观的。 You must multiply by 1E6 你必须乘以1E6

IF you want to calculate distace between 2 Geopoint by Haversine formula 如果你想通过Haversine公式计算2 Geopoint之间的距离

public class DistanceCalculator {
   // earth’s radius = 6,371km
   private static final double EARTH_RADIUS = 6371 ;
   public static double distanceCalcByHaversine(GeoPoint startP, GeoPoint endP) {
      double lat1 = startP.getLatitudeE6()/1E6;
      double lat2 = endP.getLatitudeE6()/1E6;
      double lon1 = startP.getLongitudeE6()/1E6;
      double lon2 = endP.getLongitudeE6()/1E6;
      double dLat = Math.toRadians(lat2-lat1);
      double dLon = Math.toRadians(lon2-lon1);
      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));
      return EARTH_RADIUS * c;
   }
}

distanceBetween() method will give you straight distance between two points. distanceBetween()方法将为您提供两点之间的直线距离。 Got get route distance between two points see my ansewer Here 得到两点之间的路线距离看到我的ansewer 这里

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

Geopoints have getLongitudeE6() and getLatitudeE6() to help. Geopoints有getLongitudeE6()和getLatitudeE6()来帮助。 Remember that those are E6 so you need to divide by 1E6. 请记住,那些是E6所以你需要除以1E6。

The problem with the harvesine formula is that it doesn't calculate the real distance. harvesine公式的问题在于它不计算实际距离。 It's the distance from 2 points on a sphere. 它是球体上2个点的距离。 The real distance depends on the streets, or the water ways. 真正的距离取决于街道或水路。 The harvesine formula is also a bit complicated hence it is easier to ask Google-Api to give the real distance. Harvesine公式也有点复杂,因此更容易让Google-Api给出真正的距离。 With Googlemaps Api you need to learn the directions api. 使用Googlemaps Api,您需要了解api的路线。

distanceBetween does not reffer to the real distance (the road distance) So i suggest to you to visit this google source code and it will dispaly you the real road distance between 2 geopoints. distanceBetween不会影响实际距离(道路距离)所以我建议你访问这个谷歌源代码,它会显示你2个地理点之间的真实道路距离。 Link there is 2 versions one for Android and one for blackberry check it out 链接有两个版本一个用于Android和一个用于黑莓检查出来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM