Android地图上两个位置之间的距离

Android地图上两个位置之间的距离,android,android-location,Android,Android Location,我需要计算当前位置和目的地之间的距离。我有当前位置和目的地位置的纬度和经度。我在搜索时从SO和internet上找到了以下代码。但计算结果显示,两个地点之间的距离为1366公里,而谷歌地图显示的距离为1675公里。有人能帮我计算准确的距离吗。目的地遍布全球,包括我目前所在的城市 //Distance in Kilometers fun distanceInKms ( lat1: Double, long1: Double, lat2: Double, long2: Double) : D

我需要计算当前位置和目的地之间的距离。我有当前位置和目的地位置的纬度和经度。我在搜索时从SO和internet上找到了以下代码。但计算结果显示,两个地点之间的距离为1366公里,而谷歌地图显示的距离为1675公里。有人能帮我计算准确的距离吗。目的地遍布全球,包括我目前所在的城市

//Distance in Kilometers
    fun distanceInKms ( lat1: Double, long1: Double, lat2: Double, long2: Double) : Double
    {
        val degToRad= Math.PI / 180.0;
        val phi1 = lat1 * degToRad;
        val phi2 = lat2 * degToRad;
        val lam1 = long1 * degToRad;
        val lam2 = long2 * degToRad;

        return 6371.01 * Math.acos( Math.sin(phi1) * Math.sin(phi2) + Math.cos(phi1) * Math.cos(phi2) * Math.cos(lam2 - lam1) );
    }

有人能帮我解决这个问题吗?

检查下面的示例给出准确的结果

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
        int Radius = 6371;// radius of earth in Km
        double lat1 = StartP.latitude;
        double lat2 = EndP.latitude;
        double lon1 = StartP.longitude;
        double lon2 = EndP.longitude;
        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.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult % 1000;
        int meterInDec = Integer.valueOf(newFormat.format(meter));
        Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
                + " Meter   " + meterInDec);

        return Radius * c;
    }

使用android.location.location类,该类自android中的API级别1起提供。它有一个静态的
distance-between
方法为您完成这一切

见:


除以1000以千米为单位

你可以使用它,就像谷歌别忘了添加互联网权限一样

      String getDistanceOnRoad(double latitude, double longitude,
            double prelatitute, double prelongitude) {
        String result_in_kms = "";
        String url = "http://maps.google.com/maps/api/directions/xml?             origin="
                + latitude + "," + longitude + "&destination=" + prelatitute
                + "," + prelongitude + "&mode=driving&sensor=false&units=metric";
        String tag[] = { "text" };
        HttpResponse response = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            response = httpClient.execute(httpPost, localContext);
            InputStream is = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            Document doc = builder.parse(is);
            if (doc != null) {
                NodeList nl;
                ArrayList<String> args = new ArrayList<String>();
                for (String s : tag) {
                    nl = doc.getElementsByTagName(s);
                    if (nl.getLength() > 0) {
                        Node node = nl.item(nl.getLength() - 1);
                        args.add(node.getTextContent());
                    } else {
                        args.add(" - ");
                    }
                }
                result_in_kms = String.format("%s", args.get(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result_in_kms;
    }

距离是公里吗?不是。abv计算得出1366。lat和long的值均为-37.5931551144.7216202,-27.485572153.038064。计算并查看检查您的坐标是否正确。这对我来说是完美的(我的应用程序)可能是你在mapI上选择了错误的坐标使用的距离,但同样的问题你可能使用非十进制度数格式吗?因为这些肯定是正确的。或者,你是如何使用谷歌地图精确计算的呢?也许是道路/汽车距离。你想要直线距离,还是曲面距离(圆弧、曲线)?你所说的精确距离是什么意思?
      String getDistanceOnRoad(double latitude, double longitude,
            double prelatitute, double prelongitude) {
        String result_in_kms = "";
        String url = "http://maps.google.com/maps/api/directions/xml?             origin="
                + latitude + "," + longitude + "&destination=" + prelatitute
                + "," + prelongitude + "&mode=driving&sensor=false&units=metric";
        String tag[] = { "text" };
        HttpResponse response = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            response = httpClient.execute(httpPost, localContext);
            InputStream is = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            Document doc = builder.parse(is);
            if (doc != null) {
                NodeList nl;
                ArrayList<String> args = new ArrayList<String>();
                for (String s : tag) {
                    nl = doc.getElementsByTagName(s);
                    if (nl.getLength() > 0) {
                        Node node = nl.item(nl.getLength() - 1);
                        args.add(node.getTextContent());
                    } else {
                        args.add(" - ");
                    }
                }
                result_in_kms = String.format("%s", args.get(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result_in_kms;
    }
    RouteInformations rInformation = new RouteInformations(new     AsyncResponse() {

            @Override
            public void processFinish(RouteDetails arg0) {
                // TODO Auto-generated method stub
                try
                {
                  map.addPolyline(arg0.getLineOptions()); //you can add the return line and add it to the map 

                  //here you can get distance , duration it will return like you drive a car 
                  MUT.fastDialog(Map.this,"Time and Distance","Distance : "+arg0.getDistance()+"\nDuration : "+arg0.getDuration());
              }
                catch(Exception e)
                {
                    MUT.lToast(Map.this,"Can't draw line Try Again");
                }
                }
        });

        //you should pass the 2 lat and lang which you want to draw a aline or get distance or duration between them 
         RouteDetails routeDetails=new RouteDetails();
        routeDetails.setLatLong1(from.getPosition());
        routeDetails.setLatLong2(to.getPosition());
        rInformation.execute(routeDetails);