Android Map上2个地方之间的距离

时间:2016-05-15 04:38:33

标签: android android-location

我需要计算当前位置和目的地之间的距离。我有当前和目的地位置的纬度和经度。我在搜索时从SO和互联网上找到了以下代码。但计算得出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) );
    }

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

3 个答案:

答案 0 :(得分:1)

使用自Android API级别1以来可用的distanceBetween类。它有一个静态float[] results = new float[1]; android.location.Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results); //distance in meters now in results[0] 方法为您完成所有操作。

请参阅: http://developer.android.com/reference/android/location/Location.html

  File "/Users/Chris/PycharmProjects/firstfile/trial.py", line 6, in <module>
    r = pf.read_csv('python.csv')
  File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 562, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 315, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 645, in __init__
    self._make_engine(self.engine)
  File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 799, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 1213, in __init__
    self._reader = _parser.TextReader(src, **kwds)
  File "pandas/parser.pyx", line 358, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:3427)
  File "pandas/parser.pyx", line 628, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:6861)
IOError: File python.csv does not exist

除以1000以公里(km)为单位。

答案 1 :(得分:0)

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

@session_start();
if((!isset($_SESSION['loggedIn']) || $_SESSION['loggedIn']==0) && !isset($_COOKIE['cookuhash'])){
    header("Location: /login.php");
    exit();
}

答案 2 :(得分:0)

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

      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;
    }

这个库有类来获取时间,距离和绘制折线之间的2个地方它像谷歌地图一样工作 https://github.com/memo1231014/MUT-master

库的示例

    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);