2016-05-15 61 views
2

我需要计算当前位置和目的地之间的距离。我有当前和目的地的经纬度。在搜索时,我从SO和互联网中找到了下面的代码。但计算结果为1366公里,而谷歌地图则在两个地点之间提供1675公里。有人可以帮助我如何计算准确的距离。目的地遍布世界各地,包括我目前的城市位置。Android地图上2个地点之间的距离

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

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

+0

做你想做的直线距离,或者你想表面的距离(弧,曲线)? – gian1200

+0

准确的距离是什么意思? –

回答

0

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

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

以km为单位的距离? – User

+0

是公里的距离 – Krishna

+0

不是。 abv的计算结果为1366. lat和long均为-37.5931551,144.7216202,-27.485572,153.038064。计算并看看 – User

1

使用android.location.Location类,因为在Android的API级别1可用。它有一个静态的distanceBetween方法为你做这一切。

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

float[] results = new float[1]; 
android.location.Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results); 
    //distance in meters now in results[0] 

除以1000得到它在千米(公里)。

+0

我用distanceTo但同样的问题 – User

+0

你可能使用非十进制度数格式吗?因为那些肯定是正确的。 –

+0

或者你究竟如何使用Google地图来计算它?道路/汽车的距离也许。 –

0

你可以使用它,它得到像谷歌不要忘记添加Internet权限

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